Skip to content
Snippets Groups Projects
UserService.cs 3.12 KiB
Newer Older
esantangelo's avatar
esantangelo committed
using AutoMapper;
using Microsoft.EntityFrameworkCore;
Lucca Santangelo's avatar
Lucca Santangelo committed
using System;
using System.Collections.Generic;
esantangelo's avatar
esantangelo committed
using System.Linq;
Lucca Santangelo's avatar
Lucca Santangelo committed
using System.Text;
using System.Threading.Tasks;
Lucca Santangelo's avatar
Lucca Santangelo committed
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
Lucca Santangelo's avatar
Lucca Santangelo committed
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;

namespace Tsi1.BusinessLayer.Services
{
    public class UserService : IUserService
    {
        private readonly Tsi1Context _context;

esantangelo's avatar
esantangelo committed
        private readonly IMapper _mapper;

        public UserService(Tsi1Context context, IMapper mapper)
Lucca Santangelo's avatar
Lucca Santangelo committed
        {
            _context = context;
esantangelo's avatar
esantangelo committed
            _mapper = mapper;
Lucca Santangelo's avatar
Lucca Santangelo committed
        }

esantangelo's avatar
esantangelo committed
        public async Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId)
Lucca Santangelo's avatar
Lucca Santangelo committed
        {
Lucca Santangelo's avatar
Lucca Santangelo committed
            var result = new ServiceResult<User>();
Lucca Santangelo's avatar
Lucca Santangelo committed

Lucca Santangelo's avatar
Lucca Santangelo committed
            var user = await _context.Users
                .Include(x => x.UserType)
esantangelo's avatar
esantangelo committed
                .FirstOrDefaultAsync(x => x.Username == username && x.TenantId == tenantId);
Lucca Santangelo's avatar
Lucca Santangelo committed

            if (user == null)
            {
Lucca Santangelo's avatar
Lucca Santangelo committed
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, username);
                return result;
Lucca Santangelo's avatar
Lucca Santangelo committed
            }

            if (user.Password != password)
            {
Lucca Santangelo's avatar
Lucca Santangelo committed
                result.HasError = true;
                result.Message = ErrorMessages.IncorrectPassword;
                return result;
            }

            result.Data = user;

            return result;
        }

        public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type)
        {
            var result = new ServiceResult<User>();

            var user = new User()
            {
                UserTypeId = dto.UserTypeId,
                Username = dto.Username,
                Password = dto.Password,
                Email = dto.Email,
                FirstName = dto.FirstName,
esantangelo's avatar
esantangelo committed
                LastName = dto.LastName,
                TenantId = dto.TenantId
Lucca Santangelo's avatar
Lucca Santangelo committed
            };

            if (type == UserTypes.Student)
            {
                user.Student = new Student()
                {
                    IdentityCard = dto.IdentityCard,
esantangelo's avatar
esantangelo committed
                    Age = dto.Age,
                    TenantId = dto.TenantId
Lucca Santangelo's avatar
Lucca Santangelo committed
                };
            }

            if (type == UserTypes.Professor)
            {
                user.Professor = new Professor()
                {
esantangelo's avatar
esantangelo committed
                    IdentityCard = dto.IdentityCard,
                    TenantId = dto.TenantId
Lucca Santangelo's avatar
Lucca Santangelo committed
                };
Lucca Santangelo's avatar
Lucca Santangelo committed
            }

Lucca Santangelo's avatar
Lucca Santangelo committed
            _context.Users.Add(user);
            await _context.SaveChangesAsync();
            result.Data = user;

            return result;
Lucca Santangelo's avatar
Lucca Santangelo committed
        }
esantangelo's avatar
esantangelo committed
        public async Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId)
esantangelo's avatar
esantangelo committed
        {
            var result = new ServiceResult<List<UserPreviewDto>>();

            var users = await _context.Users
esantangelo's avatar
esantangelo committed
                .Where(x => x.UserType.Name != UserTypes.FacultyAdmin && x.TenantId == tenantId)
esantangelo's avatar
esantangelo committed
                .ToListAsync();

            var usersDto = _mapper.Map<List<UserPreviewDto>>(users);

            result.Data = usersDto;

            return result;
        }
Lucca Santangelo's avatar
Lucca Santangelo committed
    }
}