public async Task <IActionResult> UpdateSite(int SiteId, SiteUpdateDTO siteUpdateDTO) { try { //check site id matches id requested if (siteUpdateDTO.SiteId != null) { if (SiteId != siteUpdateDTO.SiteId) { return(BadRequest(new { ApiProblem = "Entity Id does not match requested Id." })); } } var siteIdExists = _context.Sites.Any(s => s.SiteId == siteUpdateDTO.SiteId); var siteNameExists = _context.Sites.Any(s => s.SiteName == siteUpdateDTO.SiteName); //check if siteid does not exist if (!siteIdExists && siteUpdateDTO.SiteId != null) { //add error message ModelState.AddModelError("SiteId", "No site found with given site id."); } //check if SiteName already exists if (siteNameExists) { //check if the site is another site var theSite = _context.Sites.Where(s => s.SiteName == siteUpdateDTO.SiteName).FirstOrDefault(); if (theSite.SiteId != siteUpdateDTO.SiteId) { //add error message ModelState.AddModelError("SiteName", "SiteName already exists."); } } //try to parse given ref coordinates to npgsql box var coords = new NpgsqlBox(); if (siteUpdateDTO.RefCoordinates != null) { try { coords = NpgsqlBox.Parse(siteUpdateDTO.RefCoordinates); } catch (FormatException e) { //if exception is caught write to console and return error message Console.WriteLine("{0} Exception caught.", e); //add error message ModelState.AddModelError("RefCoordinates", "Invalid input: RefCoordinates must be specified using the following syntax \'((x1,y1),(x2,y2))\' where (x1,y1) and (x2,y2) are any two opposite corners."); return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } } if (ModelState.IsValid) { var refCoordinatesExist = _context.Sites.Any(s => s.RefCoordinates.Equals(coords)); //check if RefCoordinates already exists if (refCoordinatesExist) { //check if the site is another site var theSite = _context.Sites.Where(s => s.RefCoordinates == coords).FirstOrDefault(); if (theSite.SiteId != siteUpdateDTO.SiteId) { //add error message ModelState.AddModelError("RefCoordinates", "RefCoordinates already exist."); return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } } //find site var site = await _context.Sites.FindAsync(SiteId); //if site not found return error if (site == null) { return(NotFound()); } //update site site.SiteName = siteUpdateDTO.SiteName; site.RefCoordinates = coords; //put site try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!SiteExists(SiteId)) { return(NotFound()); } return(NoContent()); } return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } catch (NullReferenceException e) { //if exception is caught write to console and return error message Console.WriteLine("{0} Exception caught.", e); return(BadRequest(new { ApiProblem = "Invalid JSON format sent." })); } }
public async Task <ActionResult <SiteDetailsDTO> > AddSite(SiteDTO siteDTO) { try { var siteNameExists = _context.Sites.Any(s => s.SiteName == siteDTO.SiteName); //check if SiteName already exists if (siteNameExists) { //add error message ModelState.AddModelError("SiteName", "SiteName already exists."); } //try to parse given ref coordinates to npgsql box var coords = new NpgsqlBox(); if (siteDTO.RefCoordinates != null) { try { coords = NpgsqlBox.Parse(siteDTO.RefCoordinates); } catch (FormatException e) { //if exception is caught write to console and return error message Console.WriteLine("{0} Exception caught.", e); //add error message ModelState.AddModelError("RefCoordinates", "Invalid input: RefCoordinates must be specified using the following syntax \'((x1,y1),(x2,y2))\' where (x1,y1) and (x2,y2) are any two opposite corners."); return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } } if (ModelState.IsValid) { var refCoordinatesExist = _context.Sites.Any(s => s.RefCoordinates.Equals(coords)); //check if RefCoordinates already exists if (refCoordinatesExist) { //add error message ModelState.AddModelError("RefCoordinates", "RefCoordinates already exist."); return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } //create site var site = new Site { SiteName = siteDTO.SiteName, RefCoordinates = coords }; //insert site _context.Sites.Add(site); await _context.SaveChangesAsync(); //return the new site details return(CreatedAtAction( nameof(GetSiteById), new { siteId = site.SiteId }, SiteToSiteDetailsDTO(site))); } return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } catch (InvalidOperationException e) { //if exception is caught write to console and return error message Console.WriteLine("{0} Exception caught.", e); return(BadRequest(new { ApiProblem = "Invalid JSON format sent." })); } }
private object YieldJToken(Type ctype, JToken jt, int rank) { if (rank == 0) { if (ctype == typeof_BitArray) { return(Executer.Parse1010(jt.ToString())); } if (ctype == typeof_NpgsqlPoint) { return(NpgsqlPoint.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlLine) { return(NpgsqlLine.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlLSeg) { return(NpgsqlLSeg.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlBox) { return(NpgsqlBox.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlPath) { return(NpgsqlPath.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlPolygon) { return(NpgsqlPolygon.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlCircle) { return(NpgsqlCircle.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlInet) { return(new NpgsqlInet(jt.ToString())); } if (ctype == typeof_IPAddress) { return(new NpgsqlInet(jt.ToString())); } if (ctype == typeof_PhysicalAddress) { return(PhysicalAddress.Parse(jt.ToString())); } if (ctype == typeof_NpgsqlRange_int) { return(Executer.ParseNpgsqlRange <int>(jt.ToString())); } if (ctype == typeof_NpgsqlRange_long) { return(Executer.ParseNpgsqlRange <long>(jt.ToString())); } if (ctype == typeof_NpgsqlRange_decimal) { return(Executer.ParseNpgsqlRange <decimal>(jt.ToString())); } if (ctype == typeof_NpgsqlRange_DateTime) { return(Executer.ParseNpgsqlRange <DateTime>(jt.ToString())); } return(null); } return(jt.Select <JToken, object>(a => YieldJToken(ctype, a, rank - 1))); }