Exemplo n.º 1
0
        public IHttpActionResult DeleteSolutionOffers(DeleteSoltuionOfferModel offer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            SolutionOffer _offer     = null;
            string        measureId  = offer.Measure_id;
            string        solutionId = offer.Solution_id;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                _offer = db.SolutionOffers.Where(s => s.Measure_id.Equals(measureId) && s.Solution_id.Equals(solutionId))
                         .FirstOrDefault <SolutionOffer>();
                if (_offer == null)
                {
                    db.Dispose();
                    return(NotFound());
                }
                _offer.Status = "Deleted";

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    throw;
                }
            }
            return(Ok());
        }
Exemplo n.º 2
0
        public IHttpActionResult PutSolutionOffer(string id, SolutionOfferModel offer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            SolutionOffer _offer = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                _offer = db.SolutionOffers.Find(new string[] { id, offer.Solution_id });

                if (_offer == null)
                {
                    return(NotFound());
                }

                AutoMapper.Mapper.Map <SolutionOfferModel, SolutionOffer>(offer, _offer);
                _offer.Status = "Modified";

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            return(Ok());
        }
Exemplo n.º 3
0
        public IHttpActionResult PutUser(string id, AddUserModel user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            User _user = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                _user = db.Users.Find(id);

                if (_user == null)
                {
                    return(NotFound());
                }

                // Map model to entity
                Mapper.Map <AddUserModel, User>(user, _user);

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            return(Ok());
        }
Exemplo n.º 4
0
        public IHttpActionResult PostSolutionsToMeasure(AddSolutionOfferModel offer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string measureId = offer.Measure_id;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                foreach (string solutionId in offer.Solution_id)
                {
                    db.SolutionOffers.Add(new SolutionOffer
                    {
                        Measure_id  = measureId,
                        Solution_id = solutionId,
                        Status      = "Unchanged",
                    });
                }

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    throw;
                }
            }
            return(CreatedAtRoute("DefaultApi", new { id = measureId }, offer));
        }
Exemplo n.º 5
0
        public IHttpActionResult PostPlant(AddMeasureModel measure)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Measure _measure = Mapper.Map <AddMeasureModel, Measure>(measure);

            _measure.Measure_id = Guid.NewGuid().ToString("N");

            using (SoilCareEntities db = new SoilCareEntities())
            {
                db.Measures.Add(_measure);
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    if (MeasureExists(db, _measure.Measure_id))
                    {
                        return(Conflict());
                    }
                    throw;
                }
            }
            return(CreatedAtRoute("DefaultApi", new { id = _measure.Measure_id }, _measure));
        }
Exemplo n.º 6
0
        // POST telephone first: api/Users
        public User PostUserByTelephone(string telephone)
        {
            User _user = new User();

            _user.User_id        = Guid.NewGuid().ToString("N");
            _user.Created_at     = DateTime.Now;
            _user.User_telephone = telephone;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                db.Users.Add(_user);
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    if (UserExists(db, _user.User_id))
                    {
                        return(null);
                    }
                    throw;
                }
            }
            return(_user);
        }
        public IHttpActionResult PostSolution([FromBody] AddSolutionModel solution)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Solution _solution;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                string initSolutionId = Guid.NewGuid().ToString("N");
                _solution             = new Solution();
                _solution             = Mapper.Map <AddSolutionModel, Solution>(solution);
                _solution.Solution_id = initSolutionId;
                _solution.Owner       = "User";
                db.Solutions.Add(_solution);
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    if (SolutionExists(db, _solution.Solution_id))
                    {
                        return(Conflict());
                    }
                    throw;
                }
            }
            return(CreatedAtRoute("DefaultApi", new { id = _solution.Solution_id }, _solution));
        }
Exemplo n.º 8
0
        public IHttpActionResult PutLand(string id, AddLandModel land)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Land _land = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                _land = db.Lands.Find(id);

                if (_land == null)
                {
                    return(NotFound());
                }

                // Map model to entity
                _land.Land_name    = land.Land_name;
                _land.Land_image   = land.Land_image;
                _land.Land_address = land.Land_address;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            return(Ok());
        }
Exemplo n.º 9
0
        public IHttpActionResult PostLand(AddLandModel land)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Land _land = Mapper.Map <AddLandModel, Land>(land);

            _land.Land_id    = Guid.NewGuid().ToString("N");
            _land.Created_at = DateTime.Now;
            _land.Status     = "Active";

            using (SoilCareEntities db = new SoilCareEntities())
            {
                db.Lands.Add(_land);
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    if (LandExists(db, _land.Land_id))
                    {
                        return(Conflict());
                    }
                    throw;
                }
            }
            return(CreatedAtRoute("DefaultApi", new { id = _land.Land_id }, _land));
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
        // Delete: api/Lands/landId
        public IHttpActionResult DeleteLand(string id)
        {
            Land _land = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                _land = db.Lands.Find(id);
                if (_land == null)
                {
                    db.Dispose();
                    return(NotFound());
                }
                _land.Status = "Deleted";
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    throw;
                }
            }
            return(Ok());
        }