Пример #1
0
        public async Task <IHttpActionResult> Register(RideBindingModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }



                using (DunkeyContext ctx = new DunkeyContext())
                {
                    if (ctx.Rider.Any(x => x.Email == model.Email))
                    {
                        return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                        {
                            Message = "Conflict",
                            StatusCode = (int)HttpStatusCode.Conflict,
                            Result = new Error {
                                ErrorMessage = "Rider with email already exists"
                            }
                        }));
                    }
                    else
                    {
                        Rider rideModel = new Rider()
                        {
                            FullName     = model.FullName,
                            BusinessName = model.BusinessName,
                            Email        = model.Email,
                            BusinessType = model.BusinessType,
                            Status       = (int)Global.StatusCode.NotVerified,
                            Phone        = model.Phone,
                            ZipCode      = model.ZipCode
                        };

                        ctx.Rider.Add(rideModel);
                        ctx.SaveChanges();
                        using (RideViewModel rideViewModel = new RideViewModel(rideModel))
                        {
                            //userViewModel.Token = await Utility.GenerateToken(userModel.Email, userModel.Password, Request);
                            CustomResponse <RideViewModel> response = new CustomResponse <RideViewModel> {
                                Message = "Success", StatusCode = (int)HttpStatusCode.OK, Result = rideViewModel
                            };
                            return(Ok(response));
                            //return Request.CreateResponse(HttpStatusCode.OK, response);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(DunkeyDelivery.Utility.LogError(ex)));
            }
        }
Пример #2
0
        public IHttpActionResult PostNewRide(RideBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CarType carType;

            if (model.CarType == "Standard")
            {
                carType = CarType.Standard;
            }
            else
            {
                carType = CarType.Combi;
            }

            var currentUser = unitOfWork.AppUsers.FirstOrDefault(u => u.Email == User.Identity.Name && u.Deleted == false);

            AppUser driver = new AppUser();

            if (currentUser.Role == AppUser.UserRole.Admin)
            {
                driver            = unitOfWork.AppUsers.FirstOrDefault(u => u.Email == model.Driver.Email && u.Deleted == false);
                driver.DriverFree = false;

                try
                {
                    unitOfWork.AppUsers.Update(driver);
                }
                catch (Exception e)
                {
                    return(BadRequest("Error while trying to add ride to database"));
                }
            }
            Address address = new Address()
            {
                StreetName = model.StreetName, Number = model.Number, Town = model.Town, AreaCode = model.AreaCode, Deleted = false
            };

            try
            {
                unitOfWork.Addresses.Add(address);
            }
            catch (Exception e)
            {
                return(BadRequest("Error while trying to add address to database"));
            }
            var a = address;

            Location location = new Location()
            {
                XPos = 0, YPos = 0, Address = a, AddressID = a.Id, Deleted = false
            };

            try
            {
                unitOfWork.Locations.Add(location);
            }
            catch (Exception e)
            {
                return(BadRequest("Error while trying to add location to database"));
            }

            Ride newRide;

            if (currentUser.Role == AppUser.UserRole.AppUser)
            {
                newRide = new Ride()
                {
                    CarType         = carType.ToString(),
                    Status          = Status.Created,
                    Customer        = currentUser,
                    AppUserID       = currentUser.Id,
                    OrderDT         = DateTime.Now,
                    StartLocation   = location,
                    StartLocationID = location.Id,
                    Deleted         = false
                };
            }
            else
            {
                newRide = new Ride()
                {
                    CarType      = carType.ToString(),
                    Status       = Status.Formed,
                    Dispatcher   = currentUser,
                    DispatcherID = currentUser.Id,
                    //TaxiDriver = model.Driver,
                    TaxiDriverID    = driver.Id,
                    OrderDT         = DateTime.Now,
                    StartLocation   = location,
                    StartLocationID = location.Id,
                    Deleted         = false
                };
            }

            if (newRide != null)
            {
                try
                {
                    unitOfWork.Rides.Add(newRide);
                }
                catch (Exception e)
                {
                    return(BadRequest("Error while trying to add ride to database"));
                }
            }

            //OVO SAM DODALA POSLEDNJE< AKO PUCA TO JE ZBOG OVOGA! DA ZNAS
            if (currentUser.Role == AppUser.UserRole.Admin)
            {
                driver.DriverLocation   = location;
                driver.DriverLocationId = location.AddressID;

                try
                {
                    unitOfWork.AppUsers.Update(driver);
                }
                catch (Exception e)
                {
                    return(BadRequest("Error while trying to add ride to database"));
                }
            }

            unitOfWork.Complete();

            return(Ok());
        }