public async Task <IActionResult> AddInterval(int userId, [FromBody] DateTimeIntervalForCreationDto intervalForCreation) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var interval = _mapper.Map <DateTimeInterval>(intervalForCreation); if (!DateTimeIntervalValidator.Validate(interval)) { return(BadRequest("Invalid interval")); } if (userFromRepo.Intervals == null) { userFromRepo.Intervals = new List <DateTimeInterval>(); } userFromRepo.Intervals.Add(interval); if (await _repo.SaveAll()) { var intervalForReturn = _mapper.Map <DateTimeIntervalForReturnDto>(interval); return(CreatedAtRoute(nameof(GetInterval), new { id = interval.Id }, intervalForReturn)); } return(BadRequest("Couldn't add the interval")); }
public async Task <IActionResult> GetIntervals(int userId, [FromBody] DateTimeIntervalForIntersectionDto intervalForIntersection) { var intervalsFromRepo = await _repo.GetIntervals(userId); var targetInterval = _mapper.Map <DateTimeInterval>(intervalForIntersection); if (!DateTimeIntervalValidator.Validate(targetInterval)) { return(BadRequest("Invalid interval")); } var result = IntersectionCreator.CreateIntersection(intervalsFromRepo, targetInterval); var intersection = _mapper.Map <IEnumerable <DateTimeIntervalForReturnDto> >(result); return(Ok(intersection)); }