public AmzWarehouseModel(AmazonWarehouse entity) { Id = entity.Id; WarehouseCode = entity.WarehouseCode; Name = entity.Name; AddressLine = entity.AddressLine; City = entity.City; StateId = entity.State.Id; StateAbrv = entity.State.Abbreviation; ZipCode = entity.ZipCode; }
/// <summary> /// Allows for a query to be performed on the database to an AmazonWarehouse Entity. /// </summary> /// <param name="amzWarehouse">AmazonWarehouse entity to be passed to database.</param> /// <param name="dbQuery">The type of query to be performed on the database.</param> /// <returns>If successfully added to Db return True. </returns> public bool AmzWarehouseDbQuery(AmzWarehouseModel amzWarehouse, Utilities.DbQuery dbQuery) { if (amzWarehouse != null) { using (var db = new Models.AppContext()) { //instantiate new AmazonWarehouse entity and fill fields AmazonWarehouse amazon = new AmazonWarehouse() { Id = amzWarehouse.Id, WarehouseCode = amzWarehouse.WarehouseCode, Name = amzWarehouse.Name, AddressLine = amzWarehouse.AddressLine, City = amzWarehouse.City, ZipCode = amzWarehouse.ZipCode, State = db.States.Where(s => s.Id == amzWarehouse.StateId).FirstOrDefault() }; //perform correct DB query depending on what was passed in as dbentry. switch (dbQuery) { case Utilities.DbQuery.Add: db.AmazonWarehouses.Add(amazon); break; case Utilities.DbQuery.Edit: db.Entry(amazon).State = System.Data.Entity.EntityState.Modified; break; case Utilities.DbQuery.Delete: AmazonWarehouse compDelete = db.AmazonWarehouses.Find(amazon.Id); db.AmazonWarehouses.Remove(compDelete); break; } //save DbContext db.SaveChanges(); return(true); } } else { return(false); } }
/// <summary> /// Saves the shipment data to the Database. If it exists in DB, it is edited; else it's added. /// </summary> public void SaveShipmentToDB() { using (var db = new Models.AppContext()) { //create shipment entity and set props Entities.Shipment entShipment = new Shipment() { ShipmentId = Shipment.ShipmentID, Boxes = new List <ShipmentBox>(), ShipmentDate = Shipment.ShipmentDate }; //add each box one to entShipment entity foreach (var box in Shipment.Boxes) { ShipmentBox entBox = new ShipmentBox { BoxContentString = box.FBALabel(), BoxNumber = box.BoxNumber, BoxId = box.BoxID, Shipment = entShipment }; entShipment.Boxes.Add(entBox); }//end foreach loop //get amazon warehouse entity and set to ShipmentEntity AmazonWarehouse amz = db.AmazonWarehouses.Where(a => a.Id == Shipment.FullfillmentShipTo.Id).FirstOrDefault(); entShipment.ShipToCenter = amz; //get company address entity and set to shipmententity CompanyAddress comp = db.CompanyAddresses.Where(c => c.Id == Shipment.CompanyShipFrom.Id).FirstOrDefault(); entShipment.ShipFromCenter = comp; //check if shipment exists in the database if (db.Shipments.Any(s => s.ShipmentId == Shipment.ShipmentID)) { Shipment shipmentDel = db.Shipments.Find(Shipment.ShipmentID); var bx = db.Boxes.Where((b => b.Shipment.ShipmentId == shipmentDel.ShipmentId)).ToList(); for (int i = 0; i < bx.Count(); i++) { db.Boxes.Remove(bx[i]); } db.SaveChanges(); //then delete the shipment with shipmentID db.Shipments.Remove(shipmentDel); //finally add the shipment back into shipments table db.Shipments.Add(entShipment); } else { //add shipment to DB and save db.Shipments.Add(entShipment); } db.SaveChanges(); } }