aboutsummaryrefslogtreecommitdiff
path: root/API/Service/UserService.cs
blob: bd991ddfb619e567a968051cfa116c10160c920e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using API.Database;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Models.Classes;
using Models.DTOs;
using Newtonsoft.Json;

namespace API.Service
{
	public class UserService
	{
		private readonly DbRepository<User> _dbRepository;
		private readonly Mapper _userMapper;

		public UserService(DevHiveContext context, IMapper mapper)
		{
			this._dbRepository = new DbRepository<User>(context);
			this._userMapper = new Mapper
			(
			 	new MapperConfiguration
					(cfg => cfg.CreateMap<UserDTO, User>())
			 );
		}
	
		public async Task<HttpStatusCode> CreateUser(UserDTO userDTO)
		{
			//TODO: MAKE VALIDATIONS OF PROPER REQUEST

			User user = this._userMapper.Map<User>(userDTO);
			await this._dbRepository.AddAsync(user);

			return HttpStatusCode.OK;
		}

		public async Task<string> GetUserById(int id) 
		{
			User user = await this._dbRepository.FindByIdAsync(id);
			return JsonConvert.SerializeObject(user);
		}

		public async Task<HttpStatusCode> UpdateUser(int id, UserDTO userDTO)
		{
			User user = this._userMapper.Map<User>(userDTO);
		   	user.Id = id;	
			await this._dbRepository.EditAsync(id, user);

			return HttpStatusCode.OK;
		}

		public async Task<HttpStatusCode> DeleteUser(int id)
		{
			await this._dbRepository.DeleteAsync(id);

			return HttpStatusCode.OK;
		}
	}
}