public bool CreateShippingLocation(ShippingLocationDto model) { // unbox the correct object type for the credential var location = Mapper.Map <shippinglocation>(model); // set the audit logs location.CreatedBy = model.ModifiedBy; location.Created = DateTime.UtcNow; location.FromAddressDetails.CreatedBy = model.ModifiedBy; location.FromAddressDetails.Created = DateTime.UtcNow; location.ReturnAddressDetails.CreatedBy = model.ModifiedBy; location.ReturnAddressDetails.Created = DateTime.UtcNow; using (var transaction = _context.Database.BeginTransaction()) { // save the from address first var fromAddress = _context.addressdetails.Add(location.FromAddressDetails); _context.SaveChanges(); // then the return address var returnAddress = _context.addressdetails.Add(location.ReturnAddressDetails); _context.SaveChanges(); // set the address ids location.FromAddressId = fromAddress.Id; location.ReturnAddressId = returnAddress.Id; _context.shippinglocations.Add(location); _context.SaveChanges(); model.Id = location.Id; transaction.Commit(); } // if this shipping location is set to default, mark the others to False if (model.IsDefault) { var locations = _context.shippinglocations.ToList(); foreach (var item in locations) { if (item.Id == model.Id) { continue; } item.IsDefault = false; } _context.SaveChanges(); } return(true); }
public bool UpdateShippingLocation(int id, ShippingLocationDto model) { // get the curent credential var existingLocation = _context.shippinglocations.FirstOrDefault(x => x.Id == id); if (existingLocation == null) { return(false); } // unbox to the correct object for crendential Mapper.Map(model, existingLocation); existingLocation.ModifiedBy = model.ModifiedBy; existingLocation.Modified = DateTime.UtcNow; // update the from address var fromAddress = _context.addressdetails.FirstOrDefault(x => x.Id == existingLocation.FromAddressId); Mapper.Map(model.FromAddressDetails, fromAddress); fromAddress.ModifiedBy = model.ModifiedBy; fromAddress.Modified = DateTime.UtcNow; // then the return address var returnAddress = _context.addressdetails.FirstOrDefault(x => x.Id == existingLocation.ReturnAddressId); Mapper.Map(model.ReturnAddressDetails, returnAddress); returnAddress.ModifiedBy = model.ModifiedBy; returnAddress.Modified = DateTime.UtcNow; // if this shipping location is set to default, mark the others to False if (model.IsDefault) { var locations = _context.shippinglocations.ToList(); foreach (var item in locations) { if (item.Id == existingLocation.Id) { continue; } item.IsDefault = false; } } // save all the changess _context.SaveChanges(); return(true); }
public ActionResult _SaveShippingLocation(ShippingLocationDto model) { if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(e => e.Errors.Select(x => x.ErrorMessage)); return(Json(new { Error = string.Join("<br/>", errors) }, JsonRequestBehavior.AllowGet)); } model.ModifiedBy = User.Identity.Name; if (model.HasId) { _shippingService.UpdateShippingLocation(model.Id, model); } else { _shippingService.CreateShippingLocation(model); } return(Json(model, JsonRequestBehavior.AllowGet)); }