private bool IsValidRange(long placeId, OpeningTimeRequest ot, long?openingTimeId = null) { return(!_context.OpeningTimes.Any(o => o.PlaceID == placeId && ((openingTimeId.HasValue) ? o.ID != openingTimeId.Value : true) && o.Day == ot.Day && o.Open < ot.Close && o.Close > ot.Open)); }
public async Task <ActionResult <OpeningTimeResponse> > PutOpeningTime(long placeId, long openingTimeId, OpeningTimeRequest openingTimeRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!IsAuthorized(placeId)) { return(Unauthorized()); } if (!IsValidRange(placeId, openingTimeRequest, openingTimeId)) { return(BadRequest("Opening time overlaps with an existing one.")); } var openingTime = _mapper.Map <OpeningTime>(openingTimeRequest); openingTime.ID = openingTimeId; openingTime.PlaceID = placeId; _context.Entry(openingTime).State = EntityState.Modified; var placeResponse = _mapper.Map <OpeningTimeResponse>(openingTime); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OpeningTimeExists(placeId, openingTimeId)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <OpeningTimeResponse> > PostOpeningTime(long placeId, OpeningTimeRequest openingTimeRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!IsAuthorized(placeId)) { return(Unauthorized()); } if (!IsValidRange(placeId, openingTimeRequest)) { return(BadRequest("Opening time overlaps with an existing one.")); } var openingTime = _mapper.Map <OpeningTime>(openingTimeRequest); openingTime.PlaceID = placeId; _context.OpeningTimes.Add(openingTime); await _context.SaveChangesAsync(); var placeResponse = _mapper.Map <OpeningTimeResponse>(openingTime); return(CreatedAtAction(nameof(GetPlace), new { id = openingTime.ID }, placeResponse)); }