public async Task <IHttpActionResult> UpdateOrganization(string name, [FromBody] OrganizationBindingModel organization) { if (organization == null) { return(BadRequest($"Request content is empty")); } using (IUnitOfWork rep = Store.CreateUnitOfWork()) { var item = await rep.OrganizationRepository.GetAsync(name); if (item == null) { return(NotFound()); } item.Name = organization.Name.ToUpper(); item.Description = organization.Description; await rep.CompleteAsync(); var result = item.AsOrganizationDTO(Url); var response = Request.CreateResponse(HttpStatusCode.Created, result); response.Headers.Location = new Uri(result.GetUrl); return(ResponseMessage(response)); } }
public async Task <IHttpActionResult> PostOrganization( [FromBody] OrganizationBindingModel organization) { if (organization == null) { return(BadRequest($"Request content is empty")); } using (IUnitOfWork rep = Store.CreateUnitOfWork()) { var item = await rep.OrganizationRepository.GetAsync(organization.Name); if (item != null) { return(BadRequest("Organization already exists")); } var newItem = new Organization() { Name = organization.Name.ToUpper(), Description = organization.Description, Countries = new List <Country>(), }; if (organization.Countries != null) { foreach (var iso in organization.Countries) { var c = await rep.CountryRepository.GetAsync(iso); if (c == null) { return(BadRequest($"Country {iso} not found")); } newItem.Countries.Add(c); } } newItem = rep.OrganizationRepository.Add(newItem); await rep.CompleteAsync(); var result = newItem.AsOrganizationDTO(Url); var response = Request.CreateResponse(HttpStatusCode.Created, result); response.Headers.Location = new Uri(result.GetUrl); return(ResponseMessage(response)); } }