public IHttpActionResult PostPlant(AddPlantModel plant) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } string initId = Guid.NewGuid().ToString("N"); Plant _plant = Mapper.Map <AddPlantModel, Plant>(plant); _plant.Plant_id = initId; _plant.Status = "Pending"; using (SoilCareEntities db = new SoilCareEntities()) { db.Plants.Add(_plant); try { db.SaveChanges(); } catch (DbUpdateException) { if (PlantExists(db, _plant.Plant_id)) { return(Conflict()); } throw; } } return(CreatedAtRoute("DefaultApi", new { id = _plant.Plant_id }, _plant)); }
//AddPlant builds a new plant based off AddPlantModel. The information is set to the properties in PlantCare and PlantDetails. public bool AddPlant(AddPlantModel model) { PlantCare plantCare = new PlantCare { SunExposureID = model.SunExposureID, WaterNeedID = model.WaterNeedID, Temperature = model.Temperature, Description = model.PlantCareDescription, CreatedDate = DateTimeOffset.UtcNow }; ctx.PlantCare.Add(plantCare); PlantDetails plantDetails = new PlantDetails { DaysToGerminate = model.DaysToGerminate, DaysToHarvest = model.DaysToHarvest, SeedDepth = model.SeedDepth, IsPerennial = model.IsPerennial, PlantHeightMax = model.PlantHeightMax, PlantWidthMax = model.PlantWidthMax, SeedSpacing = model.SeedSpacing, RowSpacing = model.RowSpacing, RootStructureID = model.RootStructureID, IsDeerResistant = model.IsDeerResistant, IsToxicToAnimal = model.IsToxicToAnimal, IsToxicToHuman = model.IsToxicToHuman, IsMedicinal = model.IsMedicinal, Image = model.Image, Description = model.PlantDetailsDescription, CreatedDate = DateTimeOffset.UtcNow }; ctx.PlantDetails.Add(plantDetails); ctx.SaveChanges(); Plants newPlant = new Plants { Name = model.Name, ScientificName = model.ScientificName, ZoneID = model.ZoneID, SeasonID = model.SeasonID, PlantTypeID = model.PlantTypeID, PlantCareID = plantCare.PlantCareID, PlantDetailsID = plantDetails.PlantDetailsID, CreatedDate = DateTimeOffset.UtcNow }; ctx.Plants.Add(newPlant); return(ctx.SaveChanges() == 1); }
public IHttpActionResult Post(AddPlantModel plant) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreatePlantService(); if (!service.AddPlant(plant)) { return(InternalServerError()); } return(Ok("New plant has been created")); }