Esempio n. 1
0
        public async Task <ActionResult <Vehicule> > PostVehicule(VehiculeViewModel vehiculeVM)
        {
            var site = _context.Site.FirstOrDefault(s => s.Id == vehiculeVM.IdSite);

            var cles = new List <Cle>();

            if (vehiculeVM.Cles.Count > 0)
            {
                foreach (var cleVM in vehiculeVM.Cles)
                {
                    var cle = new Cle()
                    {
                        Libelle = cleVM.Libelle
                    };
                    cles.Add(cle);
                    _context.Cle.Add(cle);
                }
            }

            _context.Vehicule.Add(new Vehicule()
            {
                NumImmat  = vehiculeVM.NumImmat,
                Modele    = vehiculeVM.Modele,
                NbPlaces  = vehiculeVM.NbPlaces,
                NbPortes  = vehiculeVM.NbPortes,
                TypeCarbu = vehiculeVM.TypeCarbu,
                Actif     = vehiculeVM.Actif,
                Site      = site,
                Cles      = cles
            });

            await _context.SaveChangesAsync();

            return(Ok());
        }
Esempio n. 2
0
 public void Update(VehiculeViewModel vvm)
 {
     NumImmat  = vvm.NumImmat ?? NumImmat;
     Modele    = vvm.Modele ?? Modele;
     NbPlaces  = (vvm.NbPlaces != 0) ? vvm.NbPlaces : NbPlaces;
     NbPortes  = (vvm.NbPortes != 0) ? vvm.NbPortes : NbPortes;
     TypeCarbu = vvm.TypeCarbu ?? TypeCarbu;
     Actif     = vvm.Actif;
 }
        public async Task <IActionResult> GetVehicle([FromRoute] int id)
        {
            var token = GetToken();

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

            try
            {
                var vehicle = await _context.Vehicle.FindAsync(id);

                if (vehicle != null)
                {
                    VehiculeViewModel model = new VehiculeViewModel
                    {
                        VehId           = vehicle.VehId,
                        VehBrand        = vehicle.VehBrand,
                        VehColor        = vehicle.VehColor,
                        VehDatemec      = vehicle.VehDatemec,
                        VehIsactive     = vehicle.VehIsactive,
                        VehKm           = vehicle.VehKm,
                        VehModel        = vehicle.VehModel,
                        VehNumberplace  = vehicle.VehNumberplace,
                        VehRegistration = vehicle.VehRegistration,
                        VehTypeEssence  = vehicle.VehTypeEssence,
                        PoleId          = vehicle.VehPoleId,
                        PoleName        = _context.Pole.Any(p => p.PoleId == vehicle.VehPoleId) ? _context.Pole.SingleOrDefault(p => p.PoleId == vehicle.VehPoleId)?.PoleName : "",
                        VehState        = vehicle.VehState
                    };

                    return(Ok(model));
                }
            }
            catch (Exception e)
            {
                if (e.Source != null)
                {
                    Console.WriteLine("IOException source: {0}", e.Source);
                }
                ModelState.AddModelError("Error",
                                         "Une erreur s'est produite." + e.Message);
                return(BadRequest(ModelState));
            }

            return(NotFound());
        }
        public async Task <IActionResult> PutVehicle([FromRoute] int id, [FromBody] VehiculeViewModel vehicle)
        {
            var token = GetToken();

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


            var vehiculeToModifie = await _context.Vehicle.FindAsync(id);

            vehiculeToModifie.VehDatemec       = vehicle.VehDatemec;
            vehiculeToModifie.VehKm            = vehicle.VehKm;
            vehiculeToModifie.VehNumberplace   = vehicle.VehNumberplace;
            vehiculeToModifie.VehBrand         = vehicle.VehBrand;
            vehiculeToModifie.VehColor         = vehicle.VehColor;
            vehiculeToModifie.VehModel         = vehicle.VehModel;
            vehiculeToModifie.VehRegistration  = vehicle.VehRegistration;
            vehiculeToModifie.VehTypeEssence   = vehicle.VehTypeEssence;
            vehiculeToModifie.VehIsactive      = vehicle.VehIsactive;
            vehiculeToModifie.VehPole.PoleName = vehicle.PoleName;
            vehiculeToModifie.VehState         = (sbyte)vehicle.VehState;

            _context.Entry(vehiculeToModifie).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {
                if (!VehicleExists(id))
                {
                    return(NotFound());
                }
                if (e.Source != null)
                {
                    Console.WriteLine("IOException source: {0}", e.Source);
                }
                ModelState.AddModelError("Error",
                                         "Une erreur s'est produite lors de la modification du vehicule : ." + vehicle.VehModel);
                return(BadRequest(ModelState));
            }

            return(NoContent());
        }
Esempio n. 5
0
        public async Task <ActionResult <VehiculeViewModel> > GetVehicule(Guid id)
        {
            var vehicule = new VehiculeViewModel(await _context.Vehicule
                                                 .Include(v => v.Cles)
                                                 .Include(v => v.Site)
                                                 .FirstOrDefaultAsync(v => v.Id == id));

            if (vehicule == null)
            {
                return(NotFound());
            }

            return(vehicule);
        }
Esempio n. 6
0
        public async Task <IActionResult> PutVehicule(Guid id, VehiculeViewModel vehiculeVM)
        {
            if (!_context.Vehicule.Any(v => v.Id == id))
            {
                return(BadRequest());
            }

            var vehicule = _context.Vehicule.FirstOrDefault(s => s.Id == id);

            var cles = new List <Cle>();

            if (vehiculeVM.Cles.Count > 0)
            {
                foreach (var cleVM in vehiculeVM.Cles)
                {
                    var cle = _context.Cle.FirstOrDefault(c => c.Id == cleVM.Id);
                    cle.Update(cleVM);
                    cles.Add(cle);
                }
                vehicule.Cles = cles;
            }

            vehicule.Update(vehiculeVM);

            _context.Entry(vehicule).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VehiculeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }
        public async Task <IActionResult> PostVehicle([FromBody] VehiculeViewModel vehicle)
        {
            var token = GetToken();

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

            var vehiculeToAdd = new Vehicle()
            {
                VehBrand        = vehicle.VehBrand,
                VehColor        = vehicle.VehColor,
                VehModel        = vehicle.VehModel,
                VehRegistration = vehicle.VehRegistration,
                VehTypeEssence  = vehicle.VehTypeEssence,
                VehDatemec      = vehicle.VehDatemec,
                VehKm           = vehicle.VehKm,
                VehNumberplace  = vehicle.VehNumberplace,
                VehIsactive     = vehicle.VehIsactive,
                VehPoleId       = vehicle.PoleId,
                VehState        = (sbyte)Enums.VehiculeState.Available,
            };

            try
            {
                _context.Vehicle.Add(vehiculeToAdd);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                ModelState.AddModelError("Error", "Une erreur est survenue lors de la modification du véhicule. " + ex.Message);
                return(BadRequest(ModelState));
            }
        }
        public async Task <ActionResult> AddVehicule(VehiculeViewModel Vehi, ImageModelView userImage)
        {
            List <string> brandList = new List <string>();

            brandList.Add(Vehi.BrandAccessoryAutoVehicule);
            brandList.Add(Vehi.BrandAccessoryBikeVehicule);

            brandList.Add(Vehi.BrandVehiculeAuto);
            brandList.Add(Vehi.BrandVehiculeBike);


            string Brand = null;

            foreach (var brand in brandList)
            {
                if (brand != null)
                {
                    Brand = brand;
                }
            }
            List <string> ModeleList = new List <string>();

            ModeleList.Add(Vehi.ModelVehiculeAutoEquipment);
            ModeleList.Add(Vehi.ModelVehiculeBike);
            ModeleList.Add(Vehi.ModelVehiculeBikeEquipment);
            ModeleList.Add(Vehi.ModelVehiculeHuyndrai);
            ModeleList.Add(Vehi.ModelVehiculeKia);
            ModeleList.Add(Vehi.ModelVehiculeMazda);
            ModeleList.Add(Vehi.ModelVehiculeMercedes);
            ModeleList.Add(Vehi.ModelVehiculeToyota);


            string Model = null;

            foreach (var modell in ModeleList)
            {
                if (modell != null)
                {
                    Model = modell;
                }
            }

            if (Vehi.SearchOrAskJobVehicule == "Je vends")
            {
                Vehi.SearchOrAskJobVehicule = "J'offre";
            }

            bool isLookAuKwat = false;
            bool isParticuler = false;
            bool isPromotion  = false;

            if (User.IsInRole(MyRoleConstant.RoleAdmin) || (User.IsInRole(MyRoleConstant.Role_SuperAgent)))
            {
                isLookAuKwat = true;
                isPromotion  = true;
            }
            else
            {
                isParticuler = true;
            }

            if (Vehi.Stock == 0)
            {
                Vehi.Stock = 1;
            }

            VehiculeModel model = new VehiculeModel()
            {
                id                   = Vehi.id,
                Title                = Vehi.TitleVehicule,
                Description          = Vehi.DescriptionVehicule,
                TypeVehicule         = Vehi.TypeVehicule,
                Town                 = Vehi.TownVehicule,
                Price                = Vehi.PriceVehicule,
                Street               = Vehi.StreetVehicule,
                BrandVehicule        = Brand,
                ModelVehicule        = Model,
                RubriqueVehicule     = Vehi.RubriqueVehicule,
                PetrolVehicule       = Vehi.PetrolVehicule,
                FirstYearVehicule    = Vehi.FirstYearVehicule,
                YearVehicule         = Vehi.YearVehicule,
                MileageVehicule      = Vehi.MileageVehicule,
                NumberOfDoorVehicule = Vehi.NumberOfDoorVehicule,
                ColorVehicule        = Vehi.ColorVehicule,
                GearBoxVehicule      = Vehi.GearBoxVehicule,
                DateAdd              = DateTime.UtcNow,
                SearchOrAskJob       = Vehi.SearchOrAskJobVehicule,
                StateVehicule        = Vehi.StateVehicule,
                IsActive             = true,
                ProductCountry       = Vehi.ProductCountry,
                IsLookaukwat         = isLookAuKwat,
                IsParticulier        = isParticuler,
                IsPromotion          = isPromotion,
                VideoUrl             = Vehi.VideoUrl,
                Provider_Id          = Vehi.Provider_Id,
                Stock_Initial        = Vehi.Stock,
                Stock                = Vehi.Stock,
            };
            string success = null;

            if (ModelState.IsValid)
            {
                using (var httpClient = new HttpClient())
                {
                    string          userId = User.Identity.GetUserId();
                    ApplicationUser user   = dal.GetUserByStrId(userId);

                    var fullAddress = $"{ model.Street /*+ model.Town + "," + ",Cameroon"*/}";
                    var response    = await httpClient.GetAsync("https://api.opencagedata.com/geocode/v1/json?q=" + fullAddress + "&key=a196040df44a4a41a471173aed07635c");

                    if (response.IsSuccessStatusCode)
                    {
                        var jsonn = await response.Content.ReadAsStringAsync();

                        var joo  = JObject.Parse(jsonn);
                        var latt = (string)joo["results"][0]["geometry"]["lat"];
                        var lonn = (string)joo["results"][0]["geometry"]["lng"];

                        List <ImageProcductModel> images = ImageAdd(userImage);

                        model.Images   = images;
                        model.User     = user;
                        model.Category = new CategoryModel {
                            CategoryName = "Vehicule"
                        };
                        dal.AddVehicule(model, latt, lonn);
                        //check if email or phone is confirm and update date of publish announce for agent pay
                        if ((user.EmailConfirmed == true || user.PhoneNumberConfirmed == true) && user.Date_First_Publish == null)
                        {
                            dal.Update_Date_First_Publish(user);
                        }

                        // success = "Annonce ajoutée avec succès dans la liste !";
                        //return RedirectToAction("UserProfile", "Home", new { message = success });
                        // return RedirectToAction("GetListProductByUser_PartialView", "User");
                        return(RedirectToAction("AddImage", "Job", new { id = model.id }));
                    }
                }
            }
            success = "Désolé une erreur s'est produite!";
            return(RedirectToAction("UserProfile", "Home", new { message = success }));
        }