Example #1
0
        public async Task <IActionResult> AskLocation([FromBody] LocationViewModel model)
        {
            var token = GetToken();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!TokenService.ValidateToken(token) || !TokenService.VerifDateExpiration(token))
            {
                return(Unauthorized());
            }


            AuthService service = new AuthService(_context);
            User        user    = service.GetUserConnected(token);

            if (UserAlreadyHaveALocation(model, user))
            {
                ModelState.AddModelError("Error", "Il existe déjà une location enregistrée à votre nom durant cette période.");
                return(BadRequest(ModelState));
            }
            Location location = new Location();

            // information commentaire
            Comments comment = new Comments();

            comment.CommentDate   = DateTime.Now;
            comment.CommentText   = model.Comments;
            comment.CommentUserId = user.UserId;

            // information location
            location.LocDatestartlocation = Convert.ToDateTime(model.DateDebutResa.ToString("yyyy-MM-dd"));
            location.LocDateendlocation   = Convert.ToDateTime(model.DateFinResa.ToString("yyyy-MM-dd"));
            location.LocPoleIdstart       = model.PoleIdDepart;
            location.LocPoleIdend         = model.PoleIdDestination;
            location.LocUserId            = user.UserId;
            location.LocState             = Convert.ToSByte(Enums.LocationState.Asked);

            try
            {
                // un commentaire a besoin d'être d'abord rattacher a une location
                _context.Location.Add(location);
                await _context.SaveChangesAsync();

                comment.CommentLocId = location.LocId;
                _context.Comments.Add(comment);
                await _context.SaveChangesAsync();

                PoleService servicePole = new PoleService(_context);
                var         poleDepart  = servicePole.GetPole(location.LocPoleIdstart).PoleName;
                var         poleArrive  = servicePole.GetPole(location.LocPoleIdend).PoleName;
                string      myFiles     = System.IO.File.ReadAllText(ConstantsEmail.LocationAsk);

                myFiles = myFiles.Replace("%%USERNAME%%", user.UserFirstname);
                myFiles = myFiles.Replace("%%DEBUTLOCATION%%", location.LocDatestartlocation.ToString("dd/MM/yyyy"));
                myFiles = myFiles.Replace("%%FINLOCATION%%", location.LocDateendlocation.ToString("dd/MM/yyyy"));
                myFiles = myFiles.Replace("%%DEPARTPOLE%%", poleDepart);
                myFiles = myFiles.Replace("%%FINPOLE%%", poleArrive);
                var response = await EmailService.SendEmailAsync("Vous venez de demander une Location - BookYourCar", myFiles, user.UserEmail);

                if (response.IsSuccessStatusCode)
                {
                    return(Ok());
                }
                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)
            {
                _logger.LogError(ex.Message);
                ModelState.AddModelError("Error", "Une erreur est survenue lors de votre demande de location.");
                return(BadRequest(ModelState));
            }
        }
Example #2
0
        public async Task <IActionResult> DeleteLocation([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string token = GetToken();

            // cas ou l'administrateur rejette la demande
            if (TokenService.ValidateTokenWhereIsAdmin(token) && TokenService.VerifDateExpiration(token))
            {
                Location location = await _context.Location.FindAsync(id);

                if (location == null)
                {
                    return(NotFound());
                }
                ////////
                location.LocState = (sbyte)Enums.LocationState.Rejected;

                AuthService service     = new AuthService(_context);
                User        user        = service.GetUserConnected(token);
                PoleService servicePole = new PoleService(_context);
                var         poleDepart  = servicePole.GetPole(location.LocPoleIdstart).PoleName;
                var         poleArrive  = servicePole.GetPole(location.LocPoleIdend).PoleName;
                string      myFiles     = System.IO.File.ReadAllText(ConstantsEmail.LocationRefuser);

                myFiles = myFiles.Replace("%%USERNAME%%", user.UserFirstname);
                myFiles = myFiles.Replace("%%DEBUTLOCATION%%", location.LocDatestartlocation.ToString("dd/MM/yyyy"));
                myFiles = myFiles.Replace("%%FINLOCATION%%", location.LocDateendlocation.ToString("dd/MM/yyyy"));
                myFiles = myFiles.Replace("%%DEPARTPOLE%%", poleDepart);
                myFiles = myFiles.Replace("%%FINPOLE%%", poleArrive);
                var response = await EmailService.SendEmailAsync("Refus de votre location - BookYourCar", myFiles, user.UserEmail);

                if (response.IsSuccessStatusCode)
                {
                    // on change l'état de disponiblite du vehicule à Available
                    var locationVehicule = _context.Vehicle.SingleOrDefault(x => x.VehId == location.LocVehId);
                    locationVehicule.VehState = (sbyte)Enums.VehiculeState.Available;
                    // on desafecte le vehicule
                    location.LocVehId = null;
                    _context.Update(locationVehicule);
                    _context.SaveChanges();
                }



                _context.Location.Update(location);
                _context.SaveChanges();

                return(Ok());
            }

            // cas ou l'utilisateur annule sa demande
            else if (TokenService.ValidateToken(token) && TokenService.VerifDateExpiration(token))
            {
                Location location = await _context.Location.FindAsync(id);

                if (location == null)
                {
                    return(NotFound());
                }
                ////////
                location.LocState = (sbyte)Enums.LocationState.Canceled;

                _context.Location.Update(location);
                _context.SaveChanges();

                return(Ok());
            }

            return(Unauthorized());
        }