Example #1
0
        public async Task <IActionResult> CreateAsync([FromBody] InLogEntryDTO entryDTO)
        {
            var    userId   = HttpContext.User.GetUserId();
            double?overtime = null;

            if (!userId.HasValue)
            {
                return(Unauthorized());
            }

            if (entryDTO.Type == DayType.WorkDay)
            {
                if (!entryDTO.EndTime.HasValue || entryDTO.StartTime > entryDTO.EndTime)
                {
                    return(BadRequest());
                }

                overtime = (DateTimeOffset.FromUnixTimeSeconds(entryDTO.EndTime.Value).UtcDateTime
                            - DateTimeOffset.FromUnixTimeSeconds(entryDTO.StartTime).UtcDateTime
                            - TimeSpan.FromHours(8)).TotalHours;

                overtime = overtime < 0 ? null : overtime;
            }
            else if (entryDTO.Type == DayType.DayOff)
            {
                if (!entryDTO.VacationId.HasValue)
                {
                    return(BadRequest());
                }

                var vacation = await _repositoryManager.VacationRepository
                               .GetAsync(userId.Value, entryDTO.VacationId.Value);

                if (vacation == null)
                {
                    return(NotFound());
                }

                if (!vacation.Active || vacation.MaximumDays == 0 ||
                    vacation.UsedDays >= vacation.MaximumDays)
                {
                    return(BadRequest());
                }

                vacation.UsedDays += 1;
            }

            var entry = LogEntryMapper.Map(userId.Value, entryDTO, overtime);

            entry = _repositoryManager.LogEntryRepository.Create(entry);

            await _repositoryManager.SaveAsync();

            return(Ok(LogEntryMapper.Map(entry)));
        }
Example #2
0
 public static LogEntry Map(int userId, InLogEntryDTO entryDTO, double?overtime)
 {
     return(new LogEntry
     {
         UserId = userId,
         Type = (int)entryDTO.Type,
         VacationId = entryDTO.VacationId,
         StartTime = DateTimeOffset.FromUnixTimeSeconds(entryDTO.StartTime).UtcDateTime,
         EndTime = entryDTO.EndTime.HasValue
         ? DateTimeOffset.FromUnixTimeSeconds(entryDTO.EndTime.Value).UtcDateTime
         : (DateTime?)null,
         Overtime = overtime,
         Comment = entryDTO.Comment,
         CreatedAt = DateTime.UtcNow
     });
 }