public async Task <VacationType> AddAsync(VacationTypeDto typeDto) { var existingType = _urlopikDbContext.VacationTypes.SingleOrDefault(x => x.Name == typeDto.Name); if (existingType != null) { if (existingType.IsDeleted) { existingType.IsDeleted = false; await _urlopikDbContext.SaveChangesAsync(); return(existingType); } else { throw new ApiException($"VacationType {typeDto.Name} already exists!"); } } var newType = _mapper.Map <VacationType>(typeDto); await _urlopikDbContext.AddAsync(newType); await _urlopikDbContext.SaveChangesAsync(); return(newType); }
public async Task RegisterAsync(RegisterDto registerDto) { if (IsEmailAlreadyInUse(registerDto.Email)) { throw new ApiException($"Email: {registerDto.Email} already in use!"); } if (registerDto.SupervisorId.HasValue && !_urlopikDbContext.Users.Any(x => x.Id == registerDto.SupervisorId)) { throw new ApiException($"Supervisor: {registerDto.SupervisorId} does not exist!"); } var salt = new byte[HashSaltSize]; RNGCryptoServiceProvider.GetBytes(salt); var hashedPassword = CreatePasswordHash(registerDto.Password, salt); var domainAccount = _mapper.Map <User>(registerDto); domainAccount.PasswordHash = hashedPassword; domainAccount.Salt = salt; await _urlopikDbContext.Users.AddAsync(domainAccount); await _urlopikDbContext.SaveChangesAsync(); }
public async Task <VacationViewModel> AddAsync(VacationDto vacationDto) { var user = _httpContext.GetUserUsingClaimsOrThrow(_urlopikDbContext); ValidateVactionTypeOrThrow(vacationDto.TypeId); var vacation = _mapper.Map <Vacation>(vacationDto); vacation.VacationerId = user.Id; vacation.HrAccepted = user.Role == Roles.Hr; await _urlopikDbContext.AddAsync(vacation); await _urlopikDbContext.SaveChangesAsync(); return(_mapper.Map <VacationViewModel>(vacation)); }