public async Task <ActionResult> ReplaceSlip(Guid id, [FromBody] SlipEntity replacementSlip) { if (id == null || id == Guid.Empty) { return(BadRequest()); } if (replacementSlip == null) { return(BadRequest("The body must contain Json.")); } if (replacementSlip.Number == 0) { return(BadRequest("The slip id cannot be 0.")); } if (replacementSlip.Id != Guid.Empty) { return(BadRequest("The id cannot be included.")); } if ((await _slipService.GetAsync()).Any(x => x.Number == replacementSlip.Number)) { return(BadRequest($"The request tried to replace with a slip that already exists.")); } replacementSlip.Id = id; SlipEntity slip; try { slip = await _slipService.GetAsync(id); } catch (DocumentClientException ex) { if (ex.Message.Contains("Resource Not Found")) { return(BadRequest("Slip does not exist")); } } try { await _slipService.UpsertAsync(replacementSlip); return(Ok(new SlipResponseEntity(replacementSlip))); } catch { return(StatusCode(500)); } }
public async Task <ActionResult> DeleteBoat(Guid id) { if (id == null || id == Guid.Empty) { return(BadRequest()); } BoatEntity boat; try { boat = await _boatService.GetAsync(id); } catch (DocumentClientException ex) { if (ex.Message.Contains("Resource Not Found")) { return(BadRequest("Boat does not exist")); } return(StatusCode(500)); } //Check and see if we have a slip that we need to remove the boat from SlipEntity slip = (await _slipService.GetAsync()).SingleOrDefault(x => x.CurrentBoat == id); try { await _boatService.DeleteAsync(boat.Id); if (slip != null) { slip.CurrentBoat = null; slip.ArrivalDate = null; await _slipService.UpsertAsync(slip); } return(Ok()); } catch { return(StatusCode(500)); } }
public async Task <ActionResult> AddSlip([FromBody] SlipEntity slip) { if (slip == null) { return(BadRequest("The body must contain Json.")); } if (slip.Number == 0) { return(BadRequest($"The request body must have a number for the slip that is 1 or greater.")); } if (slip.Id != Guid.Empty) { return(BadRequest("The id cannot be included.")); } if ((await _slipService.GetAsync()).Any(x => x.Number == slip.Number)) { return(BadRequest($"The request tried to add a slip number that already exists.")); } slip.Id = Guid.NewGuid(); slip.CurrentBoat = null; slip.ArrivalDate = null; try { await _slipService.UpsertAsync(slip); return(Ok(new SlipResponseEntity(slip))); } catch { return(StatusCode(500)); } }
public async Task <ActionResult> Arrive(Guid id, [FromBody] SlipEntity entity) { if (entity.Id == Guid.Empty && entity.Number == 0) { return(BadRequest("Either a slip id or a slip number must be provided.")); } if (!entity.ArrivalDate.HasValue) { entity.ArrivalDate = DateTime.Now; } SlipEntity retrievedSlip; if (entity.Id != Guid.Empty) { try { retrievedSlip = await _slipService.GetAsync(entity.Id); } catch (DocumentClientException ex) { if (ex.Message.Contains("Resource Not Found")) { return(BadRequest("Slip does not exist")); } return(StatusCode(500)); } catch { return(StatusCode(500)); } if (entity.Number != 0 && entity.Number != retrievedSlip.Number) { return(BadRequest("Slip number/id mismatch!.")); } } else { try { retrievedSlip = (await _slipService.GetAsync()).SingleOrDefault(x => x.Number == entity.Number); } catch { return(StatusCode(500)); } if (retrievedSlip == null) { return(BadRequest($"Slip with number {entity.Number} does not exist.")); } } if (retrievedSlip.CurrentBoat != null) { return(StatusCode(403, $"Slip with number {entity.Number} is occupied!")); } BoatEntity retrievedBoat; try { retrievedBoat = await _boatService.GetAsync(id); } catch (DocumentClientException ex) { if (ex.Message.Contains("Resource Not Found")) { return(BadRequest("Boat does not exist")); } return(StatusCode(500)); } catch { return(StatusCode(500)); } if (!retrievedBoat.AtSea) { return(BadRequest("Boat must sail to sea before docking at another slip.")); } retrievedSlip.CurrentBoat = id; retrievedSlip.ArrivalDate = entity.ArrivalDate; retrievedBoat.AtSea = false; List <Task> completionObject = new List <Task> { _slipService.UpsertAsync(retrievedSlip), _boatService.UpsertAsync(retrievedBoat) }; await Task.WhenAll(completionObject); return(Ok()); }