Skip to content
Snippets Groups Projects
UserService.cs 9.13 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;
        }

esantangelo's avatar
esantangelo committed
        public async Task<ServiceResult<User>> Create(UserRegisterDto dto, string type, int tenantId)
Lucca Santangelo's avatar
Lucca Santangelo committed
        {
            var result = new ServiceResult<User>();

esantangelo's avatar
esantangelo committed
            var user = _mapper.Map<User>(dto);
esantangelo's avatar
esantangelo committed

esantangelo's avatar
esantangelo committed
            user.TenantId = 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,
Lucca Santangelo's avatar
Lucca Santangelo committed
                    TenantId = tenantId
Lucca Santangelo's avatar
Lucca Santangelo committed
                };
            }

            if (type == UserTypes.Professor)
            {
                user.Professor = new Professor()
                {
esantangelo's avatar
esantangelo committed
                    IdentityCard = dto.IdentityCard,
Lucca Santangelo's avatar
Lucca Santangelo committed
                    TenantId = 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<bool>> Modify(UserModifyDto dto, string type, int userId)
        {
            var result = new ServiceResult<bool>();

            var user = await _context.Users
                .Include(x => x.Student)
                .Include(x => x.Professor)
                .FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            if (dto.Username != user.Username)
            {
                var existingUser = await _context.Users
                    .FirstOrDefaultAsync(x => x.Username == dto.Username 
                        && x.TenantId == user.TenantId);

                if (existingUser != null)
                {
                    result.HasError = true;
                    result.Message = string.Format(ErrorMessages.DuplicateUsername, dto.Username);
                    return result;
                }
            }

            _mapper.Map(dto, user);

            if (type == UserTypes.Student)
            {
                user.Student.IdentityCard = dto.IdentityCard;
                user.Student.Age = dto.Age;
            }

            if (type == UserTypes.Professor)
            {
                user.Professor.IdentityCard = dto.IdentityCard;
            }

            await _context.SaveChangesAsync();

            result.Data = true;

            return result;
        }

        public async Task<ServiceResult<List<UserDetailDto>>> GetAll(int tenantId)
            var result = new ServiceResult<List<UserDetailDto>>();
esantangelo's avatar
esantangelo committed

            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<UserDetailDto>>(users);
esantangelo's avatar
esantangelo committed

            result.Data = usersDto;

            return result;
        }
        public async Task<ServiceResult<UserDetailDto>> GetById(int userId)
esantangelo's avatar
esantangelo committed
        {
            var result = new ServiceResult<UserDetailDto>();
esantangelo's avatar
esantangelo committed

            var user = await _context.Users
                .Include(x => x.UserType)
                .Include(x => x.Student)
                .Include(x => x.Professor)
                .FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            var userType = user.UserType.Name;

            if (userType == UserTypes.Student && user.Student == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.StudentDoesNotExist, userId);
                return result;                
            }
            else if(userType == UserTypes.Professor && user.Professor == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.ProffesorDoesNotExist, userId);
                return result;
            }

            var userDto = _mapper.Map<UserDetailDto>(user);
esantangelo's avatar
esantangelo committed

            result.Data = userDto;

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

        public async Task<ServiceResult<User>> GetByUsername(string username, int tenantId)
        {
            var result = new ServiceResult<User>();

            var user = await _context.Users
                .Include(x => x.UserType)
                .Include(x => x.Student)
                .Include(x => x.Professor)
                .Where(x => x.TenantId == tenantId
                    && x.Username == username)
                .FirstOrDefaultAsync();

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, username);
                return result;
            }

            var userType = user.UserType.Name;

            if (userType == UserTypes.Student && user.Student == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.StudentDoesNotExist, username);
                return result;
            }
            else if (userType == UserTypes.Professor && user.Professor == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.ProffesorDoesNotExist, username);
                return result;
            }

            result.Data = user;
            return result;
        }

        public async Task<ServiceResult<bool>> UpdatePassword(int userId, string password)
        {
            var result = new ServiceResult<bool>();
            var user = await _context.Users.FirstOrDefaultAsync();

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            user.Password = password;
            await _context.SaveChangesAsync();

            return result;
        }
esantangelo's avatar
esantangelo committed

        public async Task<ServiceResult<int>> GetTenant(int userId)
        {
            var result = new ServiceResult<int>();

            var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            result.Data = user.TenantId;

            return result;
        }

        public async Task<ServiceResult<bool>> Delete(int userId)
        {
            var result = new ServiceResult<bool>();

            var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
                return result;
            }

            _context.Users.Remove(user);

            await _context.SaveChangesAsync();

            result.Data = true;

            return result;
        }

        public async Task<ServiceResult<UserTypeDto>> GetUserType(int userId)
        {
            var result = new ServiceResult<UserTypeDto>();

            var user = await _context.Users
                .Include(x => x.UserType)
                .FirstOrDefaultAsync(x => x.Id == userId);

            if (user == null)
            {
                result.HasError = true;
                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
            }

            result.Data = _mapper.Map<UserTypeDto>(user.UserType);

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