/// <summary> /// Converts this instance of <see cref="LocationHistory"/> to an instance of <see cref="LocationHistoryDTO"/>. /// </summary> /// <param name="entity"><see cref="LocationHistory"/> to convert.</param> public static LocationHistoryDTO ToDTO(this LocationHistory entity) { if (entity == null) { return(null); } var dto = new LocationHistoryDTO(); dto.ID = entity.ID; dto.LocationID = entity.LocationID; dto.Name = entity.Name; dto.ModifiedByUserID = entity.ModifiedByUserID; dto.ModifiedDate = entity.ModifiedDate; dto.AuditDate = entity.AuditDate; entity.OnDTO(dto); return(dto); }
/// <summary> /// Converts this instance of <see cref="LocationHistoryDTO"/> to an instance of <see cref="LocationHistory"/>. /// </summary> /// <param name="dto"><see cref="LocationHistoryDTO"/> to convert.</param> public static LocationHistory ToEntity(this LocationHistoryDTO dto) { if (dto == null) { return(null); } var entity = new LocationHistory(); entity.ID = dto.ID; entity.LocationID = dto.LocationID; entity.Name = dto.Name; entity.ModifiedByUserID = dto.ModifiedByUserID; entity.ModifiedDate = dto.ModifiedDate; entity.AuditDate = dto.AuditDate; dto.OnEntity(entity); return(entity); }
public async Task <ActionResult <LocationHistoryDetailsDTO> > AddLocationToHistory(LocationHistoryDTO locationHistoryDTO) { try { var cartIdExists = _context.Carts.Any(c => c.CartId == locationHistoryDTO.CartId); var siteIdExists = _context.Sites.Any(s => s.SiteId == locationHistoryDTO.SiteId); //check if cartid exists if (!cartIdExists && locationHistoryDTO.CartId != null) { //add error message ModelState.AddModelError("CartId", "No cart found with given cart id."); } //check if siteid exists if (!siteIdExists && locationHistoryDTO.SiteId != null) { //add error message ModelState.AddModelError("SiteId", "No site found with given site id."); } //if model is not valid return error messages if (!ModelState.IsValid) { return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } //tries to parse cart coordinates to NpgsqlPoint. if exception, return bad request var coords = new NpgsqlPoint();; try { coords = NpgsqlPoint.Parse(locationHistoryDTO.CartCoordinates); } 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("CartCoordinates", "Invalid input: CartCoordinates must be specified using the following syntax \'(x,y)\' where x and y are the respective coordinates, as floating-point numbers."); return(BadRequest(ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToArray()))); } //create location history var locationHistory = new LocationHistory { CartId = locationHistoryDTO.CartId, SiteId = locationHistoryDTO.SiteId, CartCoordinates = coords, RecordDate = DateTime.Now }; //insert location history _context.LocationHistories.Add(locationHistory); await _context.SaveChangesAsync(); //rerturn the new location history details return(CreatedAtAction( nameof(GetLocationHistoryByRecordId), new { recordId = locationHistory.RecordId }, LocationHistoryToLocationHistoryDetailsDTO(locationHistory))); } 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." })); } }
/// <summary> /// Invoked when <see cref="ToEntity"/> operation is about to return. /// </summary> /// <param name="entity"><see cref="LocationHistory"/> converted from <see cref="LocationHistoryDTO"/>.</param> static partial void OnEntity(this LocationHistoryDTO dto, LocationHistory entity);
/// <summary> /// Invoked when <see cref="ToDTO"/> operation is about to return. /// </summary> /// <param name="dto"><see cref="LocationHistoryDTO"/> converted from <see cref="LocationHistory"/>.</param> static partial void OnDTO(this LocationHistory entity, LocationHistoryDTO dto);