//[Authorize(Roles="Admin,Manager,AppUser,Client,NotAuthenticated")]
        //[AllowAnonymous]
        public IHttpActionResult PostRents(RentBindingModel rentBM)
        {
            lock (unitOfWork.Rents)
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                AppUser appu     = new AppUser();
                var     appusers = unitOfWork.AppUsers.GetAll();

                foreach (var i in appusers)
                {
                    if (i.Username == User.Identity.Name)
                    {
                        appu = i;
                        break;
                    }
                }

                Vehicle veh      = new Vehicle();
                var     vehicles = unitOfWork.Vehicles.GetAll();

                foreach (var v in vehicles)
                {
                    if (v.Id == rentBM.VehicleId)
                    {
                        veh = v;
                        break;
                    }
                }


                Rent rent = new Rent()
                {
                    Start       = DateTime.Parse(rentBM.Start),
                    End         = DateTime.Parse(rentBM.End),
                    Deleted     = false,
                    VehicleId   = rentBM.VehicleId,
                    GetBranchId = rentBM.GetBranchId,
                    RetBranchId = rentBM.RetBranchId
                };

                appu.Renting.Add(rent);
                veh.Unvailable = true;

                unitOfWork.Rents.Add(rent);
                unitOfWork.AppUsers.Update(appu);
                unitOfWork.Vehicles.Update(veh);
                unitOfWork.Complete();

                return(CreatedAtRoute("DefaultApi", new { id = rent.Id }, rent));
            }
        }
예제 #2
0
        public IHttpActionResult PostRent(RentBindingModel rent)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var vehicle = unitOfWork.Vehicle.Get(rent.Vehicle);

            if (rent.End <= DateTime.Now)
            {
                return(BadRequest("End time is lower then start time"));
            }

            if (vehicle.Unavailable == true)
            {
                return(BadRequest("Vehicle is in use"));
            }
            else
            {
                vehicle.Unavailable = true;
            }

            var branch = unitOfWork.Branch.Get(rent.Branch);

            Rent rr = new Rent()
            {
                Branch = branch, Vehicle = vehicle, Start = DateTime.Now, End = rent.End
            };

            var user = unitOfWork.AppUser.Get(rent.User);

            user.Rents.Add(rr);

            unitOfWork.AppUser.Update(user);
            unitOfWork.Vehicle.Update(vehicle);
            unitOfWork.Rent.Add(rr);
            unitOfWork.Complete();

            return(CreatedAtRoute("DefaultApi", new { id = rr.Id }, rent));
        }
예제 #3
0
        public IHttpActionResult PostRent(RentBindingModel rent)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var users = unitOfWork.AppUser.GetAll();

            var vehicle = unitOfWork.Vehicle.Get(rent.Vehicle);

            bool found = false;

            foreach (var item in users)
            {
                foreach (var item2 in item.Rents)
                {
                    if (item2.Vehicle == vehicle)
                    {
                        if (rent.Start >= item2.Start && rent.Start <= item2.End)
                        {
                            found = true;
                        }
                        else if (rent.End >= item2.Start && rent.End <= item2.End)
                        {
                            found = true;
                        }
                        else if (rent.Start <= item2.Start && rent.End >= item2.End)
                        {
                            found = true;
                        }
                    }
                }
            }

            if (!found)
            {
                if (rent.End <= rent.Start)
                {
                    return(BadRequest("End time is lower then start time"));
                }

                if (vehicle.Unavailable == true)
                {
                    return(BadRequest("Vehicle is in use"));
                }
                else
                {
                    vehicle.Unavailable = true;
                }

                var branch = unitOfWork.Branch.Get(rent.Branch);

                Rent rr = new Rent()
                {
                    Branch = branch, Vehicle = vehicle, Start = rent.Start, End = rent.End
                };

                var user = unitOfWork.AppUser.Get(rent.User);

                user.Rents.Add(rr);

                unitOfWork.AppUser.Update(user);
                unitOfWork.Vehicle.Update(vehicle);
                unitOfWork.Rent.Add(rr);
                unitOfWork.Complete();

                return(CreatedAtRoute("DefaultApi", new { id = rr.Id }, rent));
            }
            else
            {
                return(BadRequest("This vehicle is already reserver at the that time."));
            }
        }
예제 #4
0
        public IHttpActionResult PostRent(RentBindingModel rentBindingModel)
        {
            lock (o)
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                bool found = false;

                var branches = unitOfWork.Branches.GetAll();

                Branch branch      = new Branch();
                Branch branchStart = new Branch();

                foreach (var item in branches)
                {
                    if (item.Address == rentBindingModel.Branch)
                    {
                        branch = item;
                    }
                }

                foreach (var item in branches)
                {
                    if (item.Address == rentBindingModel.BranchStart)
                    {
                        branchStart = item;
                    }
                }

                var vehicles = unitOfWork.Vehicles.GetAll();

                Vehicle vehicle = new Vehicle();

                foreach (var item in vehicles)
                {
                    if (item.Id == rentBindingModel.Vehicle.Id)
                    {
                        vehicle = item;
                    }
                }

                var username = User.Identity.Name;

                var users = unitOfWork.AppUsers.GetAll();

                List <DateTime> startDates = new List <DateTime>();
                List <DateTime> endDates   = new List <DateTime>();

                foreach (var item in users)
                {
                    foreach (var item2 in item.Rents)
                    {
                        if (item2.Vehicle == vehicle)
                        {
                            startDates.Add(item2.Start);
                            endDates.Add(Convert.ToDateTime(item2.End));
                            if (rentBindingModel.Start >= item2.Start && rentBindingModel.Start <= item2.End)
                            {
                                //onda ne moze
                                found = true;
                            }
                            else if (rentBindingModel.End >= item2.Start && rentBindingModel.End <= item2.End)
                            {
                                //onda ne moze
                                found = true;
                            }
                            else if (rentBindingModel.Start <= item2.Start && rentBindingModel.End >= item2.End)
                            {
                                //onda ne moze
                                found = true;
                            }
                        }
                    }
                }

                if (!found)
                {
                    AppUser appUser = new AppUser();

                    foreach (var item in users)
                    {
                        if (item.Email == rentBindingModel.Email)
                        {
                            appUser = item;
                        }
                    }

                    Rent rent = new Rent();
                    rent.Branch      = branch;
                    rent.BranchStart = branchStart;
                    rent.End         = rentBindingModel.End;
                    rent.Start       = rentBindingModel.Start;
                    rent.Vehicle     = vehicle;
                    //rent.Paid = false;

                    appUser.Rents.Add(rent);
                    unitOfWork.AppUsers.Update(appUser);
                    unitOfWork.Rents.Add(rent);
                    unitOfWork.Complete();

                    return(CreatedAtRoute("DefaultApi", new { id = rent.Id }, rent));
                }

                else
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append("This vehicle is already reserved by another users in these period of times: ");
                    for (int i = 0; i < startDates.Count; i++)
                    {
                        stringBuilder.AppendLine("Start date: " + startDates[i] + ", end date: " + endDates[i]);
                    }
                    return(BadRequest(stringBuilder.ToString()));
                }
            }
        }