public int AddSickLeaves(SickLeaves data)
        {
            var res = _context.SickLeaves.Add(data);

            _context.SaveChanges();
            return(res.Entity.id);
        }
        private CalendarEventWithAdditionalData CreateCalendarEventFromSickLeave(SickLeaves sickLeave)
        {
            var completed = sickLeave.SickLeaveCompletes
                            .Select(c => new CalendarEventWithAdditionalData.SickLeaveCompletion(c.ById.ToString(), c.At))
                            .FirstOrDefault();

            var cancelled = sickLeave.SickLeaveCancellations
                            .Select(c => new CalendarEventWithAdditionalData.SickLeaveCancellation(c.ById.ToString(), c.At))
                            .FirstOrDefault();

            var statuses = new[]
            {
                Tuple.Create(completed?.Timestamp, SickLeaveStatuses.Completed),
                Tuple.Create(cancelled?.Timestamp, SickLeaveStatuses.Cancelled)
            };

            var status = statuses
                         .Where(x => x.Item1 != null)
                         .OrderByDescending(x => x.Item1)
                         .FirstOrDefault()
                         ?.Item2;

            status = status ?? SickLeaveStatuses.Requested;

            var calendarEvent = new CalendarEvent(
                this.calendarEventIdParser.GetCalendarEventIdFromCspId(sickLeave.Id, CalendarEventTypes.Sickleave),
                CalendarEventTypes.Sickleave,
                new DatesPeriod(sickLeave.Start.Date, sickLeave.End.Date),
                status,
                sickLeave.EmployeeId.ToString());

            return(new CalendarEventWithAdditionalData(calendarEvent, cancelled, completed));
        }
        private SickLeaves CreateSickLeaveFromCalendarEvent(
            CalendarEvent @event,
            DateTimeOffset timestamp,
            string updatedBy)
        {
            var sickLeave = new SickLeaves
            {
                EmployeeId = int.Parse(@event.EmployeeId),
                RaisedAt   = timestamp,
                Start      = @event.Dates.StartDate.Date,
                End        = @event.Dates.EndDate.Date
            };

            var updatedById = int.Parse(updatedBy);

            if (@event.Status == SickLeaveStatuses.Cancelled)
            {
                var sickLeaveCancellation = new SickLeaveCancellations
                {
                    At   = timestamp,
                    ById = updatedById
                };
                sickLeave.SickLeaveCancellations.Add(sickLeaveCancellation);
            }

            return(sickLeave);
        }
 private void EnsureSickLeaveIsNotCompleted(SickLeaves sickLeave)
 {
     if (sickLeave.SickLeaveCompletes.Any())
     {
         throw new Exception($"Sick leave with id {sickLeave.Id} have been already completed and cannot be changed");
     }
 }
        public bool UpdateSickLeaves(int id, SickLeaves data)
        {
            var res = _context.SickLeaves.FirstOrDefault(x => x.id == id);

            if (res == null)
            {
                return(false);
            }
            res.disablementIncapacityReasonId = data.disablementIncapacityReasonId;
            res.accrualId = data.accrualId;
            res.dateStart = data.dateStart;
            res.dateEnd   = data.dateEnd;
            _context.SaveChanges();
            return(true);
        }
        public IActionResult PutSickLeaves(int id, [FromBody] SickLeaves data)
        {
            try
            {
                var res = _repository.UpdateSickLeaves(id, data);

                if (!res)
                {
                    return(Conflict(409));
                }

                return(Ok(res));
            }
            catch (Exception error)
            {
                return(BadRequest(error));
            }
        }
        public IActionResult PostSickLeaves(SickLeaves data)
        {
            try
            {
                var res = _repository.AddSickLeaves(data);

                if (res == -1)
                {
                    return(Conflict(409));
                }

                return(Ok(res));
            }
            catch (Exception error)
            {
                return(BadRequest(error));
            }
        }