Exemplo n.º 1
0
        private HttpResponseMessage AddUserFarm(HttpRequestMessage request, UserFarmDTO cqDto, int contactId, string key, int companyId, int userId)
        {
            var aur = new UserFarmRepository();
            var ur  = new UserFarm();

            //ur = aur.GetByUserFarmIds(contactId, int.Parse(cqDto.FarmID));
            if (cqDto.FarmID == "0")
            {
                var fr = new FarmRepository();
                foreach (Farm f in fr.GetFarms())
                {
                    ur.FarmId   = f.FarmId;
                    ur.UserId   = contactId;
                    ur.StatusId = 1;
                    aur.Save(ur);
                }
            }
            else
            {
                ur.FarmId   = int.Parse(cqDto.FarmID);
                ur.UserId   = contactId;
                ur.StatusId = 1;
                aur.Save(ur);
            }

            cqDto.Key = key;
            return(request.CreateResponse(HttpStatusCode.Accepted, cqDto));
        }
Exemplo n.º 2
0
        public ActionResult Create(FarmFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("Create", viewModel));
            }

            var userId = User.Identity.GetUserId();
            var farm   = new Farm
            {
                Name = viewModel.Name,
                Url  = viewModel.Url
            };

            var userFarm = new UserFarm
            {
                FarmId = farm.Id,
                UserId = userId
            };

            _unitOfWork.Farms.Add(farm);
            _unitOfWork.UserFarms.Add(userFarm);
            _unitOfWork.Complete();

            var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var user        = userManager.FindById(userId);

            if (viewModel.IsDefault || user.DefaultFarmId == 0)
            {
                user.DefaultFarmId = farm.Id;
                userManager.Update(user);
            }

            return(RedirectToAction("Details", new { url = farm.Url }));
        }
        /// <summary>
        /// Get List of Farms by User
        /// Included: IrrigationUnitList; User; City;
        /// </summary>
        /// <param name="pUser"></param>
        /// <returns></returns>
        public List <Farm> GetFarmListIncludedIrrigationUnitListCityBy(User pUser)
        {
            List <Farm> lReturn   = null;
            List <Farm> lFarmList = null;
            UserFarm    lUserFarm = null;

            if (pUser != null)
            {
                lFarmList = db.Farms
                            .Include(f => f.IrrigationUnitList)
                            .Include(f => f.City)
                            .Include(f => f.UserFarmList).ToList();
                foreach (Farm item in lFarmList)
                {
                    lUserFarm = item.UserFarmList.Where(uf => uf.UserId == pUser.UserId).FirstOrDefault();
                    if (lUserFarm != null)
                    {
                        if (lReturn == null)
                        {
                            lReturn = new List <Farm>();
                        }
                        lReturn.Add(item);
                    }
                }
            }
            return(lReturn);
        }
Exemplo n.º 4
0
        public ActionResult DeleteConfirmed(long id)
        {
            UserFarm userFarm = db.UserFarms.Find(id);

            db.UserFarms.Remove(userFarm);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 5
0
        public ActionResult Edit([Bind(Include = "UserId,Name,Surname,Phone,Address,Email,UserName,Password,RoleId, Enable, FarmIdSelected")] EditUserViewModel user)
        {
            if (ModelState.IsValid)
            {
                MD5 md5Hash = MD5.Create();

                User updatedUser = db.Users.Find(user.UserId);

                if (updatedUser == null)
                {
                    return(HttpNotFound());
                }

                //user.Password = CryptoUtils.GetMd5Hash(md5Hash, user.Password);
                updatedUser.Name    = user.Name;
                updatedUser.Surname = user.Surname;
                updatedUser.Phone   = user.Phone;
                updatedUser.Address = user.Address;
                updatedUser.Email   = user.Email;
                updatedUser.RoleId  = user.RoleId;
                updatedUser.Enable  = user.Enable;

                db.Entry(updatedUser).State = EntityState.Modified;
                db.SaveChanges();

                //Save Relation whit Farm
                if (user.FarmIdSelected != null)
                {
                    string[] farmIds = user.FarmIdSelected.Split('|');

                    UserFarmConfiguration ufc           = new UserFarmConfiguration();
                    List <UserFarm>       userFarmsList = ufc.GetUserFarmRelatedListBy(updatedUser);

                    foreach (UserFarm userFarms in userFarmsList)
                    {
                        db.Entry(userFarms).State = EntityState.Deleted;
                    }
                    db.SaveChanges();

                    foreach (var farmId in farmIds)
                    {
                        Farm lFarm = db.Farms.Find(int.Parse(farmId));

                        UserFarm lUserFarm = new UserFarm();
                        lUserFarm.UserId    = user.UserId;
                        lUserFarm.FarmId    = int.Parse(farmId);
                        lUserFarm.Name      = updatedUser.Name + " " + lFarm.Name;
                        lUserFarm.StartDate = DateTime.Now;
                        lFarm.UserFarmList.Add(lUserFarm);
                        db.UserFarms.Add(lUserFarm);
                    }
                    db.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            return(View("~/Views/Security/Users/Edit.cshtml", user));
        }
Exemplo n.º 6
0
 public ActionResult Edit([Bind(Include = "UserFarmId,UserId,FarmId,Name,StartDate")] UserFarm userFarm)
 {
     if (ModelState.IsValid)
     {
         db.Entry(userFarm).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.FarmId = new SelectList(db.Farms, "FarmId", "Name", userFarm.FarmId);
     ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", userFarm.UserId);
     return(View(userFarm));
 }
Exemplo n.º 7
0
        public void GetUserFarm_CorrectUserIdAndFarmId_ShouldBeReturned()
        {
            var userFarm = new UserFarm {
                FarmId = 1, UserId = "1"
            };

            _mockUserFarms.SetSource(new[] { userFarm });

            var userFarmFromDb = _repository.GetUserFarm(userFarm.FarmId, userFarm.UserId);

            userFarmFromDb.Should().BeOfType <UserFarm>();
        }
Exemplo n.º 8
0
        public void GetUserFarm_DifferentUserId_ShouldNotBeReturned()
        {
            var userFarm = new UserFarm {
                FarmId = 1, UserId = "1"
            };

            _mockUserFarms.SetSource(new[] { userFarm });

            var userFarmFromDb = _repository.GetUserFarm(1, "2");

            userFarmFromDb.Should().BeNull();
        }
Exemplo n.º 9
0
        public void GetAllByFarmId_UserFarmWithOtherId_ShouldNotBeReturned()
        {
            var userFarm = new UserFarm {
                FarmId = 1
            };

            _mockUserFarms.SetSource(new[] { userFarm });

            var userFarmFromDb = _repository.GetAllByFarmId(2);

            userFarmFromDb.Should().BeEmpty();
        }
Exemplo n.º 10
0
        public void GetAllByFarmId_UserFarmWithId_UserFarmShouldBeReturned()
        {
            var userFarm = new UserFarm {
                FarmId = 1
            };

            _mockUserFarms.SetSource(new[] { userFarm });

            var userFarmFromDb = _repository.GetAllByFarmId(userFarm.FarmId);

            userFarmFromDb.Should().Contain(userFarm);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Return if the UserId exists in UserFarm List,
        /// else null
        /// </summary>
        /// <param name="pUser"></param>
        /// <returns></returns>
        public UserFarm ExistUserFarm(UserFarm pUserFarm)
        {
            UserFarm lReturn = null;

            foreach (UserFarm item in this.UserFarmList)
            {
                if (item.Equals(pUserFarm))
                {
                    lReturn = item;
                    break;
                }
            }
            return(lReturn);
        }
Exemplo n.º 12
0
        // GET: UserFarms/Details/5
        public ActionResult Details(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserFarm userFarm = db.UserFarms.Find(id);

            if (userFarm == null)
            {
                return(HttpNotFound());
            }
            return(View(userFarm));
        }
Exemplo n.º 13
0
        public ActionResult Wizard([Bind(Include = "UserId,Name,Surname,Phone,Address,Email,UserName,Password,ConfirmPassword,RoleId,FarmsHidden")] CreateUserViewModel user)
        {
            if (ModelState.IsValid)
            {
                MD5 md5Hash = MD5.Create();


                //  private string[] s = user.FarmsHidden

                var userMapped = new User
                {
                    Address  = user.Address,
                    Email    = user.Email,
                    Name     = user.Name,
                    Password = user.Password,
                    Phone    = user.Phone,
                    RoleId   = user.RoleId,
                    Surname  = user.Surname,
                    UserName = user.UserName
                };
                userMapped.Password = CryptoUtils.GetMd5Hash(md5Hash, user.Password);
                db.Users.Add(userMapped);



                //working with farms
                string[] farmsId = user.FarmsHidden.Split('|');
                foreach (string fId in farmsId)
                {
                    UserFarm lUserFarm = new UserFarm();
                    Farm     lfarm     = new Farm();
                    long     id        = long.Parse(fId);
                    lfarm = (from farm in db.Farms
                             where farm.FarmId == id
                             select farm).FirstOrDefault();
                    lUserFarm.User      = userMapped;
                    lUserFarm.FarmId    = id;
                    lUserFarm.Name      = userMapped.Name + " - " + lfarm.Name;
                    lUserFarm.StartDate = DateTime.Now;

                    db.UserFarms.Add(lUserFarm);
                }


                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View("~/Views/Security/Users/Wizard.cshtml", user));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Update the UserFarm from User Farm List,
        /// If do not exists return null
        /// </summary>
        /// <param name="pUser"></param>
        /// <param name="pFarm"></param>
        /// <param name="pName"></param>
        /// <param name="pStartDate"></param>
        /// <returns></returns>
        public UserFarm UpdateUserFarm(User pUser, Farm pFarm,
                                       String pName, DateTime pStartDate)
        {
            UserFarm lReturn   = null;
            UserFarm lUserFarm = new UserFarm(this.GetNewUserFarmListId(), pUser, pFarm, pName, pStartDate);

            lReturn = ExistUserFarm(lUserFarm);
            if (lReturn != null)
            {
                lReturn.UserId    = pUser.UserId;
                lReturn.FarmId    = pFarm.FarmId;
                lReturn.Name      = pName;
                lReturn.StartDate = pStartDate;
            }
            return(lReturn);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Add a UserFarm to User Farm List,
        /// If exists return null
        /// </summary>
        /// <param name="pUser"></param>
        /// <param name="pFarm"></param>
        /// <param name="pName"></param>
        /// <param name="pStartDate"></param>
        /// <returns></returns>
        public UserFarm AddUserFarm(User pUser, Farm pFarm,
                                    String pName, DateTime pStartDate)
        {
            UserFarm lReturn       = null;
            long     lUserFarmId   = this.GetNewUserFarmListId();
            String   lUserFarmName = pUser.Name + pFarm.Name;
            UserFarm lUserFarm     = new UserFarm(lUserFarmId, pUser, pFarm,
                                                  lUserFarmName, DateTime.Now);

            if (ExistUserFarm(lUserFarm) == null)
            {
                this.UserFarmList.Add(lUserFarm);
                lReturn = lUserFarm;
            }
            return(lReturn);
        }
Exemplo n.º 16
0
        // GET: UserFarms/Edit/5
        public ActionResult Edit(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            UserFarm userFarm = db.UserFarms.Find(id);

            if (userFarm == null)
            {
                return(HttpNotFound());
            }
            ViewBag.FarmId = new SelectList(db.Farms, "FarmId", "Name", userFarm.FarmId);
            ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", userFarm.UserId);
            return(View(userFarm));
        }
Exemplo n.º 17
0
        /// <summary>
        /// Add a UserFarm to User Farm List creating new User,
        /// If exists return null
        /// </summary>
        /// <param name="pName"></param>
        /// <param name="pSurname"></param>
        /// <param name="pPhone"></param>
        /// <param name="pAddress"></param>
        /// <param name="pEmail"></param>
        /// <param name="pRoleId"></param>
        /// <param name="pUserName"></param>
        /// <param name="pPassword"></param>
        /// <returns></returns>
        public UserFarm AddUser(long pUserId, String pName, String pSurname, String pPhone, String pAddress,
                                String pEmail, long pRoleId, String pUserName, String pPassword)
        {
            UserFarm lReturn     = null;
            long     lUserFarmId = this.GetNewUserFarmListId();
            User     lUser       = new User(pUserId, pName, pSurname, pPhone,
                                            pAddress, pEmail, pRoleId,
                                            pUserName, pPassword);
            String   lUserFarmName = lUser.Name + this.Name;
            UserFarm lUserFarm     = new UserFarm(lUserFarmId, lUser, this,
                                                  lUserFarmName, DateTime.Now);

            if (ExistUserFarm(lUserFarm) == null)
            {
                this.UserFarmList.Add(lUserFarm);
                lReturn = lUserFarm;
            }
            return(lReturn);
        }
Exemplo n.º 18
0
        private HttpResponseMessage RemoveUserFarm(HttpRequestMessage request, UserFarmDTO cqDto, int contactId, string key, int companyId, int userId)
        {
            var aur = new UserFarmRepository();
            var ur  = new UserFarm();

            if (cqDto.FarmID == "0")
            {
                foreach (UserFarm uf in aur.GetByUserId(int.Parse(cqDto.UserID)))
                {
                    aur.Delete(uf);
                }
            }
            else
            {
                ur = aur.GetByUserFarmIds(contactId, int.Parse(cqDto.FarmID));
                aur.Delete(ur);
            }

            cqDto.Key = key;
            return(request.CreateResponse(HttpStatusCode.Accepted, cqDto));
        }
Exemplo n.º 19
0
        public HttpResponseMessage UpdateFarmEggsHistoryByUser(JObject obj)
        {
            var msg = PerformOperation(() =>
            {
                string sessionId = obj["sessionId"].Value<string>();
                ValidateSessionId(sessionId);
                User currentUser = db.Users.SingleOrDefault(x => x.SessionId == sessionId);

                int petId = obj["petId"].Value<int>();
                Animal pet = ValidatePetId(petId);

                string action = obj["action"].Value<string>();
                if (action != "ADDE" && action != "SELLE" && action != "LOSEE")
                {
                    throw BuildHttpResponseException("Invalid Eggs Action", "ERR_EGG_ACT");
                }

                int count = obj["count"].Value<int>();
                if (count < 0)
                {
                    throw BuildHttpResponseException("Invalid Eggs Count", "ERR_EGG_CNT");
                }

                UserFarm uf = db.UserFarms.SingleOrDefault(x => x.PetId == petId && x.UserId == currentUser.Id);

                double price = obj["price"].Value<double>();

                switch (action)
                {
                    case "ADDE":
                        {
                            if (price < 0)
                            {
                                throw BuildHttpResponseException("Invalid Eggs Price", "ERR_EGG_PRC");
                            }
                            else
                            {
                                if (uf == null)
                                {
                                    UserFarm tempUF = new UserFarm()
                                    {
                                        UserId = currentUser.Id,
                                        PetId = petId,
                                        EggsInStock = count,
                                        EggsHatching = 0,
                                        PetCount = 0
                                    };
                                    db.UserFarms.Add(tempUF);
                                }
                                else
                                {
                                    uf.EggsInStock += count;
                                }
                                UserFarmHistory tempFarmHistory = new UserFarmHistory()
                                {
                                    PetCount = 0,
                                    PetId = petId,
                                    PetStatus = action,
                                    EggsHatching = 0,
                                    EggsStocked = count,
                                    UserId = currentUser.Id,
                                    ActionDate = DateTime.Now,
                                    PetPrice = price
                                };
                                db.UserFarmHistories.Add(tempFarmHistory);
                                db.SaveChanges();
                            }
                            break;
                        }
                    case "SELLE":
                        {
                            if (price < 0)
                            {
                                throw BuildHttpResponseException("Invalid Eggs Price", "ERR_EGG_PRC");
                            }
                            else
                            {
                                if (uf == null)
                                {
                                    throw BuildHttpResponseException("Can not sell eggs of pet you dont have", "ERR_EGG_INV");
                                }
                                else
                                {
                                    uf.EggsInStock -= count;
                                    if (uf.EggsInStock < 0)
                                    {
                                        throw BuildHttpResponseException("Can not sell more eggs than you have", "ERR_HTC_CNT");
                                    }
                                }
                                UserFarmHistory tempFarmHistory = new UserFarmHistory()
                                {
                                    PetCount = 0,
                                    PetId = petId,
                                    PetStatus = action,
                                    EggsHatching = 0,
                                    EggsStocked = count,
                                    UserId = currentUser.Id,
                                    ActionDate = DateTime.Now,
                                    PetPrice = price
                                };
                                db.UserFarmHistories.Add(tempFarmHistory);
                                db.SaveChanges();
                            }
                            break;
                        }
                    case "LOSEE":
                        {
                            if (uf == null)
                            {
                                throw BuildHttpResponseException("Can not lose eggs of pet you dont have", "ERR_HTC_PET");
                            }
                            else
                            {
                                uf.EggsInStock -= count;
                                if (uf.EggsInStock < 0)
                                {
                                    throw BuildHttpResponseException("Can not lose more hatchings eggs that you have", "ERR_HTC_CNT");
                                }
                            }
                            UserFarmHistory tempFarmHistory = new UserFarmHistory()
                            {
                                PetCount = 0,
                                PetId = petId,
                                PetStatus = action,
                                EggsHatching = 0,
                                EggsStocked = count,
                                UserId = currentUser.Id,
                                ActionDate = DateTime.Now,
                                PetPrice = 0
                            };
                            db.UserFarmHistories.Add(tempFarmHistory);
                            db.SaveChanges();
                            break;
                        }
                    default:
                        break;
                }
                return "Success";
            });
            return msg;
        }
Exemplo n.º 20
0
 public void Remove(UserFarm userFarm)
 {
     _context.UserFarms.Remove(userFarm);
 }
Exemplo n.º 21
0
 public void Add(UserFarm userFarm)
 {
     _context.UserFarms.Add(userFarm);
 }