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));
        }
Example #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());
        }
        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());
        }
Example #4
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));
        }
        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));
        }
        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());
        }
        // 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);
        }
Example #8
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));
        }
Example #9
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());
        }
Example #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));
        }
Example #11
0
        // Get: api/Plants
        public IList <PlantModel> GetAllPlants()
        {
            IList <PlantModel> plantList = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                plantList = db.Plants.Where(s => s.Status.ToLower().Equals("approved"))
                            .Select(AutoMapper.Mapper.Map <Plant, PlantModel>)
                            .OrderBy(s => s.Plant_name)
                            .ToList();
            }
            return(plantList);
        }
        // Get: api/Solutions
        public IHttpActionResult GetAllSolutions()
        {
            IList <SolutionModel> listSolution = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                listSolution = db.Solutions.Where(s => s.Owner.ToLower().Equals("system"))
                               .Select(AutoMapper.Mapper.Map <Solution, SolutionModel>)
                               .ToList();
            }
            if (listSolution.Count == 0)
            {
                return(NotFound());
            }
            return(Ok(listSolution));
        }
        // Get: api/Solutions/solutionid
        public IHttpActionResult GetSolutionById(string id)
        {
            SolutionModel _solution = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                Solution solution = db.Solutions.Find(id);
                if (solution == null)
                {
                    db.Dispose();
                    return(NotFound());
                }
                _solution = Mapper.Map <Solution, SolutionModel>(solution);
            }
            return(Ok(_solution));
        }
        // Get: api/Soils
        public IHttpActionResult GetAllSoils()
        {
            IList <SoilModel> listSoil = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                listSoil = db.Soils.Include("Plants")
                           .Select(AutoMapper.Mapper.Map <Soil, SoilModel>)
                           .ToList();
            }
            if (listSoil.Count == 0)
            {
                return(NotFound());
            }
            return(Ok(listSoil));
        }
        public IHttpActionResult GetLandsByUserId(string id)
        {
            IList <LandModel> listLands = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                listLands = db.Lands.Where(s => s.User_id.Equals(id) && s.Status.ToLower().Equals("active"))
                            .Select(AutoMapper.Mapper.Map <Land, LandModel>)
                            .ToList();
            }
            if (listLands.Count == 0)
            {
                return(NotFound());
            }
            return(Ok(listLands));
        }
Example #16
0
        // Get: api/Plants/id
        public IHttpActionResult GetPlantById(string id)
        {
            PlantModel plant = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                plant = db.Plants.Include("Soil").Where(s => s.Plant_id.Equals(id))
                        .Select(AutoMapper.Mapper.Map <Plant, PlantModel>)
                        .FirstOrDefault <PlantModel>();
            }
            if (plant == null)
            {
                return(NotFound());
            }
            return(Ok(plant));
        }
        // Get: api/Users/id
        public IHttpActionResult GetUserById(string id)
        {
            UserModel user = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                user = db.Users.Include("Lands")
                       .Where(s => s.User_id.Equals(id))
                       .Select(AutoMapper.Mapper.Map <User, UserModel>)
                       .FirstOrDefault();
            }
            if (user == null)
            {
                return(NotFound());
            }
            return(Ok(user));
        }
        public IHttpActionResult GetMeasuresByLandId(string id)
        {
            IList <MeasureModel> measures = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                measures = db.Measures.Include("Plant").Include("SolutionOffers")
                           .Where(s => s.Land_id.Equals(id))
                           .Select(AutoMapper.Mapper.Map <Measure, MeasureModel>)
                           .ToList();
            }
            if (measures.Count == 0)
            {
                return(NotFound());
            }
            return(Ok(measures));
        }
        // Get: api/Lands/id
        public IHttpActionResult GetLandById(string id)
        {
            LandModel land = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                land = db.Lands
                       .Where(s => s.Land_id.Equals(id))
                       .Select(AutoMapper.Mapper.Map <Land, LandModel>)
                       .FirstOrDefault();
            }
            if (land == null)
            {
                return(NotFound());
            }
            return(Ok(land));
        }
Example #20
0
        public IHttpActionResult GetSolutionsByMeasureId(string measureid)
        {
            IList <SolutionWithStatusModel> listSolution = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                listSolution = db.SolutionOffers.Include("Solution")
                               .Where(s => s.Measure_id.Equals(measureid))
                               .Select(AutoMapper.Mapper.Map <SolutionOffer, SolutionWithStatusModel>)
                               .ToList();
            }
            if (listSolution.Count == 0)
            {
                return(NotFound());
            }
            return(Ok(listSolution));
        }
Example #21
0
        // Get: api/Measures/id
        public IHttpActionResult GetMeasureById(string id)
        {
            MeasureModel measure = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                measure = db.Measures.Include("SolutionOffers")
                          .Where(s => s.Measure_id.Equals(id))
                          .Select(AutoMapper.Mapper.Map <Measure, MeasureModel>)
                          .FirstOrDefault();
            }
            if (measure == null)
            {
                return(NotFound());
            }
            return(Ok(measure));
        }
        // Get: api/Soils/id
        public IHttpActionResult GetSoilById(string id)
        {
            SoilModel Soil = null;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                Soil = db.Soils.Include("Plants")
                       .Where(s => s.Soil_id.Equals(id))
                       .Select(AutoMapper.Mapper.Map <Soil, SoilModelDetail>)
                       .FirstOrDefault <SoilModelDetail>();
            }
            if (Soil == null)
            {
                return(NotFound());
            }
            return(Ok(Soil));
        }
        public IHttpActionResult GetUserByTelephone(string id)
        {
            User user  = null;
            bool IsNew = false;

            using (SoilCareEntities db = new SoilCareEntities())
            {
                user = db.Users.FirstOrDefault(s => s.User_telephone.Equals(id));
            }
            if (user == null)
            {
                user = PostUserByTelephone(id);
                if (user == null)
                {
                    return(Conflict());
                }
                IsNew = true;
            }
            return(Ok(new {
                user.User_id,
                IsNew,
            }));
        }
        // 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());
        }
 private bool SolutionExists(SoilCareEntities db, string id)
 {
     return(db.Solutions.Count(e => e.Solution_id.Equals(id)) > 0);
 }
Example #26
0
 private bool PlantExists(SoilCareEntities db, string id)
 {
     return(db.Plants.Count(e => e.Plant_id.Equals(id)) > 0);
 }
 private bool UserExists(SoilCareEntities db, string id)
 {
     return(db.Users.Count(e => e.User_id.Equals(id)) > 0);
 }
 private bool LandExists(SoilCareEntities db, string id)
 {
     return(db.Lands.Count(e => e.Land_id.Equals(id)) > 0);
 }
Example #29
0
 private bool MeasureExists(SoilCareEntities db, string id)
 {
     return(db.Measures.Count(e => e.Measure_id.Equals(id)) > 0);
 }