Пример #1
0
        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);
        }
Пример #2
0
        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));
        }