Exemplo n.º 1
0
        // DELETE api/<controller>/5
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                Context.Amounts.Remove(Context.Amounts.OfType <Yield>().FirstOrDefault(y => y.AmountId == id));
                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.NoContent, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 2
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] Yield yield)
        {
            try
            {
                Context.Amounts.Add(new Yield()
                {
                    Name       = yield.Name,
                    Quantity   = yield.Quantity,
                    MetricUnit = yield.MetricUnit
                });
                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 3
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, [FromBody] Yield yield)
        {
            try
            {
                var existingRecord = Context.Amounts.OfType <Yield>().FirstOrDefault(y => y.AmountId == id);

                if (existingRecord != null)
                {
                    existingRecord.Name       = yield.Name;
                    existingRecord.Quantity   = yield.Quantity;
                    existingRecord.MetricUnit = yield.MetricUnit;

                    Context.Amounts.AddOrUpdate(existingRecord);
                    Context.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK, true));
                }

                throw new Exception("Yield with id " + id + " not found.");
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 4
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] MetricUnit metricUnit)
        {
            try
            {
                Context.MetricUnits.Add(new MetricUnit()
                {
                    Name = metricUnit.Name
                });
                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 5
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, [FromBody] MetricUnit metricUnit)
        {
            try
            {
                var existingRecord = Context.MetricUnits.FirstOrDefault(mu => mu.Id == id);

                if (existingRecord != null)
                {
                    existingRecord.Name = metricUnit.Name;

                    Context.MetricUnits.AddOrUpdate(existingRecord);
                    Context.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK, true));
                }

                throw new Exception("Metric unit with id " + id + " not found.");
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 6
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, [FromBody] Planting planting)
        {
            try
            {
                var dbQuery = from p in Context.Plantings
                              .Include(p => p.PlantingCrops.Select(pc => pc.MetricUnit))
                              .Include(p => p.PlantingCrops.Select(pc => pc.Crop))
                              .Include(p => p.PlantingFertilizers.Select(pa => pa.MetricUnit))
                              .Include(p => p.PlantingFertilizers.Select(pa => pa.Fertilizer))
                              .Include(p => p.Yields)
                              .Include(p => p.Yields.Select(py => py.MetricUnit))
                              .Where(p => p.Id == id)
                              select p;

                var existingRecord = dbQuery.FirstOrDefault();

                Context.Entry(existingRecord).CurrentValues.SetValues(planting);

                if (existingRecord != null)
                {
                    existingRecord.Season = planting.Season;

                    #region Update PlantingCrops

                    var plantingCropsToRemove = existingRecord.PlantingCrops.Where(existingPlantingCrop => planting.PlantingCrops.All(pc => !ObjectComparisonHelper.AreEqual(pc, existingPlantingCrop))).ToList();

                    if (plantingCropsToRemove.Count > 0)
                    {
                        foreach (var plantingCropToRemove in plantingCropsToRemove)
                        {
                            existingRecord.PlantingCrops.Remove(plantingCropToRemove);
                        }
                    }

                    foreach (var plantingCrop in planting.PlantingCrops)
                    {
                        if (existingRecord.PlantingCrops.All(pc => !ObjectComparisonHelper.AreEqual(pc, plantingCrop)))
                        {
                            plantingCrop.Crop       = Context.Crops.FirstOrDefault(c => c.Id == plantingCrop.Crop.Id);
                            plantingCrop.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == plantingCrop.MetricUnit.Id);
                            existingRecord.PlantingCrops.Add(plantingCrop);
                        }
                    }
                    #endregion

                    #region Update PlantingFertilizers

                    var plantingFertilizersToRemove = existingRecord.PlantingFertilizers.Where(existingPlantingFertilizer => planting.PlantingFertilizers.All(pf => !ObjectComparisonHelper.AreEqual(pf, existingPlantingFertilizer))).ToList();

                    if (plantingFertilizersToRemove.Count > 0)
                    {
                        foreach (var plantingFertilizerToRemove in plantingFertilizersToRemove)
                        {
                            existingRecord.PlantingFertilizers.Remove(plantingFertilizerToRemove);
                        }
                    }

                    foreach (var plantingFertilizer in planting.PlantingFertilizers)
                    {
                        if (existingRecord.PlantingFertilizers.All(pf => !ObjectComparisonHelper.AreEqual(pf, plantingFertilizer)))
                        {
                            plantingFertilizer.Fertilizer = Context.Fertilizers.FirstOrDefault(c => c.Id == plantingFertilizer.Fertilizer.Id);
                            plantingFertilizer.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == plantingFertilizer.MetricUnit.Id);
                            existingRecord.PlantingFertilizers.Add(plantingFertilizer);
                        }
                    }
                    #endregion

                    #region Update Yields

                    var yieldsToRemove = existingRecord.Yields.Where(existingYield => planting.Yields.All(y => !ObjectComparisonHelper.AreEqual(y, existingYield))).ToList();

                    if (yieldsToRemove.Count > 0)
                    {
                        foreach (var yieldToRemove in yieldsToRemove)
                        {
                            existingRecord.Yields.Remove(yieldToRemove);
                        }
                    }

                    foreach (var yield in planting.Yields)
                    {
                        if (existingRecord.Yields.All(y => !ObjectComparisonHelper.AreEqual(y, yield)))
                        {
                            yield.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == yield.MetricUnit.Id);
                            existingRecord.Yields.Add(yield);
                        }
                    }
                    #endregion

                    var tempParcel = Context.Parcels.Include(p => p.ParcelAreas).FirstOrDefault(p => p.Id == planting.Parcel.Id);

                    if (tempParcel != null)
                    {
                        tempParcel.GruntId   = planting.Parcel.GruntId;
                        tempParcel.Name      = planting.Parcel.Name;
                        tempParcel.OwnerName = planting.Parcel.OwnerName;

                        var tempParcelAreas         = tempParcel.ParcelAreas.ToList();
                        var tempPlantingParcelAreas = planting.Parcel.ParcelAreas.ToList();
                        for (int i = 0; i < tempParcelAreas.Count; i++)
                        {
                            var muId = tempPlantingParcelAreas[i].MetricUnit.Id;
                            tempParcelAreas[i].MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == muId);
                            tempParcelAreas[i].Quantity   = tempPlantingParcelAreas[i].Quantity;
                        }
                        tempParcel.ParcelAreas = tempParcelAreas;

                        existingRecord.Parcel = tempParcel;
                    }

                    Context.Plantings.AddOrUpdate(existingRecord);
                    Context.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK, true));
                }

                throw new Exception("Planting with id " + id + " not found.");
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 7
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] Planting planting)
        {
            try
            {
                var parcelArea = planting.Parcel.ParcelAreas.FirstOrDefault();
                if (parcelArea != null && parcelArea.MetricUnit == null)
                {
                    parcelArea.MetricUnit = Context.MetricUnits.FirstOrDefault(mu =>
                                                                               mu.Id == Context.Parcels.FirstOrDefault(p =>
                                                                                                                       p.Id == planting.Parcel.Id).ParcelAreas.FirstOrDefault().MetricUnit.Id);

                    planting.Parcel.ParcelAreas = new List <ParcelArea>()
                    {
                        parcelArea
                    };
                }

                if (planting.PlantingCrops != null && planting.PlantingCrops.Count > 0)
                {
                    foreach (var plantingCrop in planting.PlantingCrops)
                    {
                        plantingCrop.Crop       = Context.Crops.FirstOrDefault(c => c.Id == plantingCrop.Crop.Id);
                        plantingCrop.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == plantingCrop.MetricUnit.Id);
                    }
                }

                if (planting.PlantingFertilizers != null && planting.PlantingFertilizers.Count > 0)
                {
                    foreach (var plantingFertilizer in planting.PlantingFertilizers)
                    {
                        plantingFertilizer.Fertilizer = Context.Fertilizers.FirstOrDefault(f => f.Id == plantingFertilizer.Fertilizer.Id);
                        plantingFertilizer.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == plantingFertilizer.MetricUnit.Id);
                    }
                }

                if (planting.Yields != null && planting.Yields.Count > 0)
                {
                    foreach (var yield in planting.Yields)
                    {
                        yield.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == yield.MetricUnit.Id);
                    }
                }

                var tempParcel = Context.Parcels.Include(p => p.ParcelAreas).FirstOrDefault(p => p.Id == planting.Parcel.Id);

                if (tempParcel != null)
                {
                    tempParcel.GruntId   = planting.Parcel.GruntId;
                    tempParcel.Name      = planting.Parcel.Name;
                    tempParcel.OwnerName = planting.Parcel.OwnerName;

                    var tempParcelAreas         = tempParcel.ParcelAreas.ToList();
                    var tempPlantingParcelAreas = planting.Parcel.ParcelAreas.ToList();
                    for (int i = 0; i < tempParcelAreas.Count; i++)
                    {
                        var muId = tempPlantingParcelAreas[i].MetricUnit.Id;
                        tempParcelAreas[i].MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == muId);
                        tempParcelAreas[i].Quantity   = tempPlantingParcelAreas[i].Quantity;
                    }
                    tempParcel.ParcelAreas = tempParcelAreas;

                    planting.Parcel = tempParcel;
                }

                Context.Plantings.AddOrUpdate(planting);
                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 8
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] Parcel parcel)
        {
            try
            {
                foreach (var parcelArea in parcel.ParcelAreas)
                {
                    parcelArea.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == parcelArea.MetricUnit.Id);
                }
                Context.Parcels.Add(parcel);

                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 9
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, [FromBody] Parcel parcel)
        {
            try
            {
                var existingRecord = Context.Parcels.FirstOrDefault(p => p.Id == id);
                if (existingRecord != null)
                {
                    if (parcel.ParcelAreas != null && parcel.ParcelAreas.Count > 0)
                    {
                        existingRecord.ParcelAreas = new List <ParcelArea>();

                        foreach (var parcelArea in parcel.ParcelAreas)
                        {
                            parcelArea.MetricUnit = Context.MetricUnits.FirstOrDefault(mu => mu.Id == parcelArea.MetricUnit.Id);
                            existingRecord.ParcelAreas.Add(parcelArea);
                        }
                    }

                    existingRecord.GruntId   = parcel.GruntId;
                    existingRecord.Name      = parcel.Name;
                    existingRecord.OwnerName = parcel.OwnerName;
                    existingRecord.Points    = parcel.Points;

                    Context.Parcels.AddOrUpdate(existingRecord);
                    Context.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK, true));
                }

                throw new Exception("Parcel with id " + id + " not found.");
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 10
0
        // DELETE api/<controller>/5
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                Context.Fertilizers.Remove(Context.Fertilizers.FirstOrDefault(f => f.Id == id));
                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.NoContent, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 11
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, [FromBody] Fertilizer fertilizer)
        {
            try
            {
                var existingRecord = Context.Fertilizers.FirstOrDefault(f => f.Id == id);
                if (existingRecord != null)
                {
                    existingRecord.Name        = fertilizer.Name;
                    existingRecord.Description = fertilizer.Description;
                    Context.Fertilizers.AddOrUpdate(existingRecord);
                    Context.SaveChanges();
                }

                return(Request.CreateResponse(HttpStatusCode.OK, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 12
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] Fertilizer fertilizer)
        {
            try
            {
                Context.Fertilizers.Add(new Fertilizer()
                {
                    Name        = fertilizer.Name,
                    Description = fertilizer.Description
                });
                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 13
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, [FromBody] Crop crop)
        {
            try
            {
                var existingRecord = Context.Crops.FirstOrDefault(c => c.Id == id);
                if (existingRecord != null)
                {
                    existingRecord.Name        = crop.Name;
                    existingRecord.Color       = crop.Color;
                    existingRecord.Description = crop.Description;
                    existingRecord.ImageUrl    = crop.ImageUrl;
                    existingRecord.ImageName   = crop.ImageName;
                    Context.Crops.AddOrUpdate(existingRecord);
                    Context.SaveChanges();
                }

                return(Request.CreateResponse(HttpStatusCode.OK, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }
Exemplo n.º 14
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] Crop crop)
        {
            try
            {
                Context.Crops.Add(new Crop()
                {
                    Name        = crop.Name,
                    Color       = crop.Color,
                    Description = crop.Description,
                    ImageUrl    = crop.ImageUrl,
                    ImageName   = crop.ImageName
                });
                Context.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, true));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ExceptionMessageHelper.GetErrorMessage(ex)));
            }
        }