예제 #1
0
        public IActionResult Update(string name)
        {
            Location loc = _locationService.GetSingle(l => l.Name == name);
            LocationUpdateViewModel vm = new LocationUpdateViewModel
            {
                Name    = loc.Name,
                Details = loc.Details,
                Active  = loc.Active,
                State   = loc.State,
                Picture = loc.Picture
            };

            return(View(vm));
        }
예제 #2
0
        public async Task <IActionResult> Update(LocationUpdateViewModel vm, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                Location location = _locationService.GetSingle(l => l.Name == vm.Name);
                location.Name    = vm.Name;
                location.State   = vm.State;
                location.Details = vm.Details;
                location.Active  = vm.Active;
                if (file != null)
                {
                    //check ??

                    //upload server path
                    var serverPath = Path.Combine(_environment.WebRootPath, "uploads");
                    //create a folder
                    Directory.CreateDirectory(Path.Combine(serverPath, User.Identity.Name));
                    //get the file name
                    string fileName = FileNameHelper.GetNameFormated(Path.GetFileName(file.FileName));

                    //stream the file to the srever
                    using (var fileStream = new FileStream(Path.Combine(serverPath, User.Identity.Name, fileName), FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    //assign the picture URL to the cat object
                    location.Picture = User.Identity.Name + "/" + fileName;
                }

                //save to db
                _locationService.Update(location);
                //go home
                return(RedirectToAction("Details", "Location", new { name = vm.Name }));
            }
            else
            {
                ViewBag.MyMessage = "Location name exists. Please change the name";
                return(View(vm));
            }
        }
예제 #3
0
        public async Task <ActionResult> Edit(string comp_id, string loc_id)
        {
            if (loc_id != null && comp_id != null)
            {
                LocationUpdateViewModel model = new LocationUpdateViewModel();
                var postTask = await client.GetAsync("/api/Location/View/?comp_id=" + comp_id + "&loc_id=" + loc_id);

                model = await postTask.Content.ReadAsAsync <LocationUpdateViewModel>();

                if (model != null)
                {
                    return(View("Edit", model));
                }
                else
                {
                    LocationIndexViewModel data = new LocationIndexViewModel();
                    ViewBag.Error = "No Location Found !";
                    return(View("Index", data));
                }
            }
            return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
        }
예제 #4
0
        public async Task <ActionResult> Update(LocationUpdateViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.active           = true;
                    model.modifiedDatetime = DateTime.Now;
                    model.modifiedBy       = Session["user_id"].ToString();
                    var postTask = await client.PostAsJsonAsync("/api/Location/Update", model);

                    if (postTask.IsSuccessStatusCode)
                    {
                        return(RedirectToAction("Index", "Location"));
                    }
                }
                catch (Exception e)
                {
                    ViewBag.Error = e.Message;
                    return(View("Update"));
                }
            }
            return(RedirectToActionPermanent("Index"));
        }
예제 #5
0
        public async Task <IActionResult> PutLocation([FromRoute] int id, [FromBody] LocationUpdateViewModel location)
        {
            #region Prerequisites
            string token = GetToken();
            if (!TokenService.ValidateToken(token) || !TokenService.VerifDateExpiration(token))
            {
                return(Unauthorized());
            }
            if (!ModelState.IsValid || id == 0)
            {
                return(BadRequest(ModelState));
            }
            Location loc = _context.Location.FirstOrDefault(l => l.LocId == id);
            if (loc == null)
            {
                return(NotFound());
            }
            #endregion

            try
            {
                LocationService locServ = new LocationService(_context);
                User            user    = _context.User.SingleOrDefault(u => u.UserId == loc.LocUserId);
                Vehicle         vehicle = _context.Vehicle.SingleOrDefault(v => v.VehId == location.VehicleId);
                Pole            poleS   = _context.Pole.SingleOrDefault(p => p.PoleId == loc.LocPoleIdstart);
                Pole            poleE   = _context.Pole.SingleOrDefault(p => p.PoleId == loc.LocPoleIdend);

                var  message1 = "";
                var  message2 = "";
                bool afficherInformationVehicule = false;

                switch (location.Action)
                {
                case "Cancel":
                    // %%MESSAGE1%% Votre location a été annulée.
                    // %%MESSAGE2%%
                    //afficher information véhicule false
                    locServ.CancelLocation(loc);
                    message1 = "Votre location a été annulée par un administrateur...";
                    message2 = "Vous pouvez le contacter pour avoir plus de détails sur cette annulation.";
                    break;

                case "Validate":
                    // %%MESSAGE1%% Bonne nouvelle votre location vient d'être validée !
                    // %%MESSAGE2%% Vous pouvez maintenant consulter le détail de votre location sur BookYourCar !
                    //afficher information véhicule true
                    locServ.ValidateLocationAndSetVehicule(loc, location.VehicleId);
                    message1 = "Bonne nouvelle, votre location vient d'être validée !";
                    message2 = "Vous pouvez maintenant consulter le détail de votre location sur BookYourCar !";
                    afficherInformationVehicule = true;
                    break;

                case "Update":
                    // %%MESSAGE1%% Votre location vient d'être modifiée !
                    // %%MESSAGE2%% Vous pouvez maintenant consulter le détail de votre location sur BookYourCar !
                    //afficher information véhicule true
                    locServ.UpdateLocationAndVehicule(loc, location.VehicleId);
                    message1 = "Votre location vient d'être modifiée !";
                    message2 = "Un nouveau véhicule vous a été affecté. Consulter le détail de votre location sur BookYourCar !";
                    afficherInformationVehicule = true;
                    break;

                case "Start":
                    // %%MESSAGE1%% Votre location vient de commencer !
                    // %%MESSAGE2%% Bonne route !
                    // afficher information véhicule true
                    locServ.StartLocation(loc);
                    message1 = "Votre location vient de commencer !";
                    message2 = "Bonne route !";
                    afficherInformationVehicule = true;
                    break;

                case "Finish":
                    // %%MESSAGE1%% Nous esperons que votre location s'est bien passée !
                    // %%MESSAGE2%% Vous pourrez retrouver le détail de votre location sur BookYourCar !
                    // afficher information véhicule false
                    message1 = "Nous espérons que votre location s'est bien passée !";
                    message2 = "Vous pourrez retrouver le détail de votre location sur BookYourCar !";
                    locServ.FinishLocation(loc);
                    break;

                default:
                    ModelState.AddModelError("Error", "L'action demandée est inconnue");
                    return(BadRequest(ModelState));
                }
                _context.Update(loc);
                await _context.SaveChangesAsync();


                if (await EmailService.SendEmailPutLocationAsync(user, loc, poleS, poleE, vehicle, message1, message2, afficherInformationVehicule))
                {
                    ModelState.AddModelError("Success", "La location a bien été modifée.");
                    return(Ok(ModelState));
                }
                else
                {
                    ModelState.AddModelError("Error",
                                             "Une erreur s'est produite sur l'envoi de mail de confirmation mais la validation de la réservation a bien été prise en compte.");
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest($"Une erreur s'est produite durant la mise à jour de la location. Cause : {ex.Message}"));
            }
        }