コード例 #1
0
        public JsonResult SendRequest(string accessToken, string startingAddressMainText, string startingAddressSecondaryText, string startingAddressLocationLat, string startingAddressLocationLng, string finishAddressMainText, string finishAddressSecondaryText, string finishAddressLocationLat, string finishAddressLocationLng)
        {
            if (HttpContext.Request.RequestType == "POST")
            {
                var newAccessToken = _accessTokenService.GenerateAccessToken(accessToken);
                if (newAccessToken == null)
                {
                    return(Json(new { status = "INVALID ACCESSTOKEN" }));
                }

                var userId = _accessTokenService.GetUserId(newAccessToken);
                if (userId == null)
                {
                    return(Json(new { status = "ERR1", accessToken = newAccessToken }));
                }
                var client = _clientService.GetAll().Where(x => x.UserId == userId).FirstOrDefault();
                if (client == null)
                {
                    return(Json(new { status = "ERR2", accessToken = newAccessToken }));
                }
                var anotherRequest = _requestService.GetClientRequest().Where(c => c.Client.Id == client.Id).FirstOrDefault();
                if (anotherRequest != null)
                {
                    return(Json(new { status = "ERR3", accessToken = accessToken }));
                }
                var requestInfo = new RequestInfo()
                {
                    CreatedBy                = CreatedBy.Client,
                    CreatedDateTime          = DateTime.Now,
                    CreatorUserId            = userId,
                    LastModificationDateTime = DateTime.Now,
                    RequestStatus            = RequestStatusEnum.NoCarChosen,
                    StartingAddress          = startingAddressMainText + " " + startingAddressSecondaryText,
                    FinishAddress            = finishAddressMainText + " " + finishAddressSecondaryText,
                    StartingLocation         = new Models.Models.Location()
                    {
                        Latitude  = double.Parse(startingAddressLocationLat),
                        Longitude = double.Parse(startingAddressLocationLng)
                    },
                    FinishLocation = new Models.Models.Location()
                    {
                        Latitude  = double.Parse(finishAddressLocationLat),
                        Longitude = double.Parse(finishAddressLocationLng)
                    }
                };
                _requestService.AddRequestInfo(requestInfo);
                var activeRequest = new ActiveRequest();
                activeRequest.Request           = requestInfo;
                activeRequest.DateTimeChosenCar = DateTime.Now;
                var appropriateCar = _carService.AppropriateCar(new Models.Models.Location()
                {
                    Latitude = double.Parse(startingAddressLocationLat), Longitude = double.Parse(startingAddressLocationLng)
                });
                if (appropriateCar != null)
                {
                    activeRequest.AppropriateCar = appropriateCar;
                    requestInfo.RequestStatus    = RequestStatusEnum.NotTaken;
                    _requestService.ModifyRequestInfo(requestInfo);
                }
                _requestService.AddActiveRequest(activeRequest);

                var clientRequest = new ClientRequest()
                {
                    Client  = client,
                    Request = requestInfo
                };
                _requestService.AddClientRequest(clientRequest);

                return(Json(new { accessToken = newAccessToken, status = "OK" }));
            }
            return(Json(new { }));
        }
コード例 #2
0
        /// <summary>
        /// Adds a new request
        /// </summary>
        /// <param name="startingAddress"></param>
        /// <param name="finishAddress"></param>
        /// <returns></returns>
        public JsonResult CreateRequest(string startingAddress, string finishAddress)
        {
            var userId     = User.Identity.GetUserId();
            var dispatcher = _dispatcherService.GetAll().First(x => x.UserId == userId);
            var company    = _companyService.GetAll().First(x => x.Id == dispatcher.Company.Id);

            var startingLocation = GoogleAPIRequest.GetLocation(startingAddress + " " + company.City);
            var finishLocation   = GoogleAPIRequest.GetLocation(finishAddress + " " + company.City);

            if (startingLocation == null)
            {
                return(Json(new { status = "INVALID STARTING LOCATION" }));
            }
            if (finishLocation == null)
            {
                finishLocation = new Models.Models.Location()
                {
                    Latitude  = 0,
                    Longitude = 0
                };
            }



            var request = new RequestInfo()
            {
                CreatedBy                = CreatedBy.Dispatcher,
                CreatorUserId            = userId,
                RequestStatus            = RequestStatusEnum.NoCarChosen,
                StartingAddress          = startingAddress,
                StartingLocation         = startingLocation,
                FinishAddress            = finishAddress,
                FinishLocation           = finishLocation,
                CreatedDateTime          = DateTime.Now,
                LastModificationDateTime = DateTime.Now,
                Company = company
            };

            _requestService.AddRequestInfo(request);
            var dashboardCell = new DispatcherDashboard()
            {
                DispatcherUserId = userId,
                LastSeen         = DateTime.Now,
                LastSeenStatus   = RequestStatusEnum.NoCarChosen,
                Request          = request
            };

            _dashboardService.AddDispatcherDashboard(dashboardCell);
            var activeRequest = new ActiveRequest()
            {
                AppropriateCar    = null,
                DateTimeChosenCar = DateTime.Now,
                Request           = request
            };

            _requestService.AddActiveRequest(activeRequest);
            var car = _carService.AppropriateCar(startingLocation, dispatcher.Company);

            if (car != null)
            {
                request.RequestStatus        = RequestStatusEnum.NotTaken;
                activeRequest.AppropriateCar = car;
                _requestService.ModifyActiveRequest(activeRequest);
                _requestService.ModifyRequestInfo(request);
            }



            return(Json(new { status = "OK" }));
        }