Пример #1
0
        public async Task <MonumentPreviewDto> Handle(UpdateMonumentCommand request, CancellationToken cancellationToken)
        {
            var monumentEntity = await _dbContext.Monuments
                                 .Include(s => s.Address)
                                 .FirstOrDefaultAsync(s => s.Id == request.MonumentId);

            if (monumentEntity is null)
            {
                throw new MonumentsManagerAppException(ExceptionType.EntityNotFound, $"Entity of type MonumentEntity with id {request.MonumentId} does not exists");
            }

            monumentEntity.FormOfProtection = request.FormOfProtection;
            monumentEntity.Name             = request.Name;
            monumentEntity.ConstructionDate = request.ConstructionDate;
            monumentEntity.Address          = new AddressEntity()
            {
                Area         = request.Address.Area,
                City         = request.Address.City,
                Commune      = request.Address.Commune,
                District     = request.Address.District,
                Province     = request.Address.Province,
                Street       = request.Address.Street,
                StreetNumber = request.Address.StreetNumber
            };

            await _dbContext.SaveChangesAsync();

            return(monumentEntity.ToPreviewDto(monumentEntity.Pictures.FirstOrDefault()));
        }
Пример #2
0
        public async Task <Unit> Handle(PromoteUserCommand request, CancellationToken cancellationToken)
        {
            var userContextEntity = await _dbContext.Users.FindAsync(_context.UserId);

            if (userContextEntity.Email == request.Email)
            {
                throw new MonumentsManagerAppException(ExceptionType.CannotPromoteYourself, "You can promote different user");
            }

            var userEntity = await _dbContext.Users.FirstOrDefaultAsync(s => s.Email == request.Email);

            if (userEntity is null)
            {
                throw new MonumentsManagerAppException(ExceptionType.EntityNotFound, $"There is no user with mail {request.Email}");
            }
            else if (userEntity.Role == UserRole.Administrator)
            {
                throw new MonumentsManagerAppException(ExceptionType.UserAlreadyPromoted, $"User {request.Email} is already promoted to Administrator role");
            }

            userEntity.Role = UserRole.Administrator;
            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
Пример #3
0
        public async Task <MonumentPreviewDto> Handle(CreateMonumentCommand request, CancellationToken cancellationToken)
        {
            var monumentEntity = new MonumentEntity()
            {
                UserId           = _context.UserId,
                FormOfProtection = request.FormOfProtection,
                ConstructionDate = request.ConstructionDate,
                Name             = request.Name,
                Address          = new AddressEntity()
                {
                    Area         = request.Address.Area,
                    City         = request.Address.City,
                    Commune      = request.Address.Commune,
                    District     = request.Address.District,
                    Province     = request.Address.Province,
                    Street       = request.Address.Street,
                    StreetNumber = request.Address.StreetNumber
                }
            };

            var result = await _dbContext.Monuments.AddAsync(monumentEntity, cancellationToken);

            await _dbContext.SaveChangesAsync();

            return(result.Entity.ToPreviewDto(new PictureEntity()));
        }
Пример #4
0
        public async Task <Unit> Handle(DeleteMonumentCommand request, CancellationToken cancellationToken)
        {
            var entity = await _dbContext.Monuments.FindAsync(request.MonumentId);

            if (entity is null)
            {
                throw new MonumentsManagerAppException(ExceptionType.EntityNotFound, $"Entity of type MonumentEntity with id {request.MonumentId} does not exists");
            }

            _dbContext.Monuments.Remove(entity);

            var addressEntity = await _dbContext.Addresses
                                .FirstOrDefaultAsync(s => s.MonumentId == request.MonumentId);

            _dbContext.Addresses.Remove(addressEntity);

            var pictures = await _dbContext.Pictures.Where(s => s.MonumentId == request.MonumentId)
                           .ToListAsync();

            _dbContext.Pictures.RemoveRange(pictures);

            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
        public async Task <int> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            if (await _dbContext.Users.AnyAsync(s => s.Email == request.Email))
            {
                throw new MonumentsManagerAppException(ExceptionType.UserAlreadyExists, $"User with email {request.Email} already exists");
            }

            if (!await _emailSender.TrySendWelcomeMailAsync(request.Email))
            {
                throw new MonumentsManagerAppException(ExceptionType.CannotSendEmail, $"Cannot send welcome mail to {request.Email}");
            }

            var entity = new UserEntity()
            {
                Password     = _passwordEncryptor.Encrypt(request.Password),
                Email        = request.Email,
                JobTitle     = request.JobTitle,
                LastLoggedIn = DateTime.UtcNow
            };

            var result = await _dbContext.AddAsync(entity, cancellationToken);

            await _dbContext.SaveChangesAsync();

            return(result.Entity.Id);
        }
        public async Task <Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            var entity = await _dbContext.Users.FindAsync(request.Id);

            if (entity is null)
            {
                throw new MonumentsManagerAppException(ExceptionType.EntityNotFound, $"Entity of type UserEntity with id {request.Id} does not exists");
            }

            entity.JobTitle  = request.JobTitle;
            entity.FirstName = request.FirstName;
            entity.LastName  = request.LastName;

            if (!string.IsNullOrEmpty(request.Password))
            {
                entity.Password = _passwordEncryptor.Encrypt(request.Password);
                var userContext = await _dbContext.Users.FindAsync(_applicationContext.UserId);

                await _emailSender.SendPasswordHasBeenChangedByAdministrator(entity.Email, userContext.Email);
            }

            _dbContext.Users.Update(entity);
            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
Пример #7
0
        public async Task <Unit> Handle(DeletePictureCommand request, CancellationToken cancellationToken)
        {
            var pictureEntity = new PictureEntity()
            {
                Id = request.PictureId
            };

            _dbContext.Pictures.Remove(pictureEntity);

            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
Пример #8
0
        public async Task <Unit> Handle(ChangePasswordByRecoveryKeyCommand request, CancellationToken cancellationToken)
        {
            var decryptedRecoveryKey = _stringEncoder.Decrypt(request.RecoveryKey);
            var encrytedUserDto      = JsonConvert.DeserializeObject <EncryptedUserDto>(decryptedRecoveryKey);

            var userEntity = await _dbContext.Users.FindAsync(encrytedUserDto.Id);

            if (userEntity is null || userEntity.Password != encrytedUserDto.Password)
            {
                throw new MonumentsManagerAppException(ExceptionType.RecoveryKeyValidationFailed, $"Recovery key is invalid for user with id {userEntity.Id}");
            }

            userEntity.Password = _passwordEncryptor.Encrypt(request.Password);
            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
        public async Task <PictureDto> Handle(CreatePictureCommand request, CancellationToken cancellationToken)
        {
            var monumentEntity = await _dbContext.Monuments
                                 .FindAsync(request.MonumentId);

            if (monumentEntity is null)
            {
                throw new MonumentsManagerAppException(ExceptionType.EntityNotFound, $"Entity of type MonumentEntity with id {request.MonumentId} does not exists");
            }

            var pictureEntity = _pictureFactory.Create(request);

            monumentEntity.Pictures.Add(pictureEntity);

            await _dbContext.SaveChangesAsync();

            return(pictureEntity.ToDto());
        }
        public async Task <Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var entity = await _dbContext.Users.FindAsync(request.Id);

            if (entity is null)
            {
                throw new MonumentsManagerAppException(ExceptionType.EntityNotFound, $"Entity of type UserEntity with id {request.Id} does not exists");
            }

            if (_context.UserId == request.Id)
            {
                throw new MonumentsManagerAppException(ExceptionType.CannotDeleteCurrentUser, $"Cannot delete current user");
            }

            _dbContext.Users.Remove(entity);
            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
Пример #11
0
        public async Task <UserDto> Handle(AuthenticateUserCommand request, CancellationToken cancellationToken)
        {
            var encryptedPassword = _passwordEncryptor.Encrypt(request.Password);

            var user = await _dbContext.Users
                       .FirstOrDefaultAsync(s => s.Email == request.Email && s.Password == encryptedPassword);

            if (user is null)
            {
                return(null);
            }

            user.LastLoggedIn = DateTime.UtcNow;
            await _dbContext.SaveChangesAsync();

            return(new UserDto()
            {
                Id = user.Id,
                JobTitle = user.JobTitle,
                Role = user.Role.ConvertTo <UserRoleDto>(),
                Email = user.Email
            });
        }