public virtual async Task <IActionResult> CreateAsync([FromBody] PeoplesV1 item, CancellationToken cancellationToken = default(CancellationToken)) { using (Logger.Perfs()) { try { if (item == null || !ModelState.IsValid) { return(BadRequest(Helper.FormatModelStateError(ModelState))); } PeoplesV1HATEOAS itemCreated = await APIService.CreateAsync(item, cancellationToken); itemCreated = WriteHATEOASLinks(this.Url, itemCreated); return(Ok(itemCreated)); } catch (APIObjAlreadyExistException e) { return(Conflict(Helper.FormatExceptionError(e))); } catch (Exception e) { return(BadRequest(Helper.FormatExceptionError(e))); } } }
public static PeoplesV1HATEOAS WriteHATEOASLinks(IUrlHelper urlHelper, PeoplesV1HATEOAS item) { item.Links.Clear(); item.Links.Add(new ApiHATEOASLink("detail", "GET", urlHelper.Link(GET_PEOPLES_BY_ID, new { peoplesId = item.Id }))); item.Links.Add(new ApiHATEOASLink("update, don't forget to put the item to update in the body", "PUT", urlHelper.Link(UPDATE_PEOPLES_ITEM, new { peoplesId = item.Id }))); item.Links.Add(new ApiHATEOASLink("delete", "DELETE", urlHelper.Link(DELETE_PEOPLES_BY_ID, new { peoplesId = item.Id }))); item.Links.Add(new ApiHATEOASLink("list, you can ask a range and/or sorting the result (ie : [link]/?range=0-10&sort=property1&desc=property2)", "GET", urlHelper.Link(GET_PEOPLES_LIST, null))); if (item.Car != null) { item.Car = CarsV1Controller.WriteCarHATEOASLinksFromPeoplesV1(urlHelper, item.Car, item.Id); } item.Links.Add(new ApiHATEOASLink("Car-create, don't forget to put the item to create in the body", "POST", urlHelper.Link(CarsV1Controller.CREATE_CAR_FROM_PEOPLESV1_ITEM, new { peoplesId = item.Id }))); foreach (var childData in item.Parties) { childData.Links.Add(new ApiHATEOASLink("Parties-remove, remove the Parties from collection", "DELETE", urlHelper.Link(REMOVE_PARTIES_BY_ID, new { peoplesId = item.Id, partiesInvolvedId = childData.Id }))); } item.Links.Add(new ApiHATEOASLink("Parties-add, add the Parties from collection, replace 0 by the id you want to add", "POST", urlHelper.Link(ADD_PARTIES_BY_ID, new { peoplesId = item.Id, partiesInvolvedId = 0 }))); return(item); }
public virtual async Task <IActionResult> GetVoterFromVotesV1Async(int electionsId, int votesId, int voterId, CancellationToken cancellationToken = default(CancellationToken)) { using (Logger.Perfs()) { try { PeoplesV1HATEOAS item = await APIService.GetVoterFromVotesV1Async(electionsId, votesId, voterId, cancellationToken); if (item == null) { return(NotFound(Helper.FormatErrorResponse(false, "Resource not found", $"Resource PeoplesV1 not found for this get request."))); } else { return(Ok(item)); } } catch (Exception e) { return(BadRequest(Helper.FormatExceptionError(e))); } } }