public IHttpActionResult PutPlantName(int id, PlantName plantName) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != plantName.PlantId) { return(BadRequest()); } db.Entry(plantName).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PlantNameExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void parseXml(Stream stream) { DateTime now = DateTime.Now; houses = new List <HouseName>(); plants = new List <PlantName>(); dates = new Dictionary <int, DateTemp>(); stream.Position = 0; XDocument xml = XDocument.Load(stream); foreach (XElement el in xml.Root.Element("houses").Elements("house")) { var house = new HouseName { name = el.Attribute("Name").Value }; houses.Add(house); foreach (XElement element in el.Elements()) { DateTemp dateTemp = new DateTemp { dateTime = getDate(element), temperature = getDouble(element, "Weather") }; TimeSpan span = (dateTemp.dateTime - DateTemp.zeroCountDateTime); dateTemp.daysNumber = span.Days; HouseDetail houseDetail = new HouseDetail { localDateTemp = now, userName = getUserName(), daysNumber = dateTemp.daysNumber, consumptionHouse = getDouble(element, "Consumption") }; if (!dates.ContainsKey(span.Days)) { dates.Add(span.Days, dateTemp); } house.listHouseDetails.Add(houseDetail); } } foreach (XElement el in xml.Root.Elements("plants").Elements()) { PlantName plant = new PlantName { name = el.Attribute("Name").Value }; plants.Add(plant); foreach (XElement element in el.Elements()) { TimeSpan span = getDate(element) - DateTemp.zeroCountDateTime; PlantDetail plantDetail = new PlantDetail { localDateTemp = now, userName = getUserName(), daysNumber = span.Days, price = getDouble(element, "Price"), consumptionPlant = getDouble(element, "Consumption") }; plant.listPlantDetails.Add(plantDetail); } } saveToDb(); }
public HttpResponseMessage PostPlant(HttpRequestMessage request, [FromBody] NewPlantDTO newPlant) { if (!ModelState.IsValid) { return(request.CreateResponse(HttpStatusCode.BadRequest, newPlant)); } try { var thisPlant = new PlantName(); thisPlant.Name = newPlant.Name; thisPlant.Sku = newPlant.Sku; db.PlantNames.Add(thisPlant); //Add the plant to the database db.SaveChanges(); //Save the changes db.Entry(thisPlant).Reload(); //reload the entry so that we are able to get the plant id var plantId = thisPlant.PlantId; //this can then be used to assign to the plant groups foreach (var group in newPlant.GroupDetails) { var thisPlantGroup = new PlantGroup(); thisPlantGroup.PlantId = plantId; thisPlantGroup.GroupId = group.GroupId; db.PlantGroups.Add(thisPlantGroup); } db.SaveChanges(); return(request.CreateResponse(HttpStatusCode.OK, newPlant)); } catch (Exception ex) { return(request.CreateResponse(HttpStatusCode.BadRequest, ex)); } }
// GET: Groups/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Group group = db.Groups.Find(id); List <PlantName> plantNames = new List <PlantName>(); var PlantsInGroup = group.PlantGroups.Where(p => p.GroupId == id); foreach (var p in PlantsInGroup) { PlantName pn = new PlantName(); pn.PlantId = p.PlantId; pn.Sku = p.PlantName.Sku; pn.Name = p.PlantName.Name; plantNames.Add(pn); } var vm = new GroupPlantVM(); vm.GroupId = group.GroupId; vm.GroupName = group.Description; vm.GroupPlants = plantNames; if (group == null) { return(HttpNotFound()); } return(View(vm)); }
public ActionResult DeleteConfirmed(int id) { PlantName plantName = db.PlantNames.Find(id); db.PlantNames.Remove(plantName); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "PlantId,Sku,Name")] PlantName plantName) { if (ModelState.IsValid) { db.Entry(plantName).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(plantName)); }
public ActionResult Create([Bind(Include = "PlantId,Sku,Name")] PlantName plantName) { if (ModelState.IsValid) { db.PlantNames.Add(plantName); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(plantName)); }
public IHttpActionResult GetPlantName(int id) { PlantName plantName = db.PlantNames.Find(id); if (plantName == null) { return(NotFound()); } return(Ok(plantName)); }
public IHttpActionResult PostPlantName(PlantName plantName) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.PlantNames.Add(plantName); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = plantName.PlantId }, plantName)); }
// GET: PlantNames/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PlantName plantName = db.PlantNames.Find(id); if (plantName == null) { return(HttpNotFound()); } return(View(plantName)); }
public IHttpActionResult DeletePlantName(int id) { PlantName plantName = db.PlantNames.Find(id); if (plantName == null) { return(NotFound()); } db.PlantNames.Remove(plantName); db.SaveChanges(); return(Ok(plantName)); }