コード例 #1
0
        public JsonResult <List <Car> > GetAllCars()
        {
            CarEntities db   = new CarEntities();
            List <Car>  cars = db.Cars.ToList();

            return(Json(cars));
        }
コード例 #2
0
        public async Task <bool> UpdateCarItemAsync(UpdateCarItemModel value, string userId)
        {
            var carEntity = await CarEntities.Find(x => x.UserId == userId && x.IsCurrent == true).FirstOrDefaultAsync();

            if (carEntity != null)
            {
                //await this.SetTotalRideAsync(new UpdateTotalRideModel
                //{
                //    Id = carEntity.Id,
                //    TotalRide = carEntity.CarsTotalRide
                //}, userId);

                var carItem = CarItems.Find(x => x.Id == value.Id).FirstOrDefault();

                var filterCarItem = Builders <CarItem> .Filter.Eq("CarItemId", value.Id);

                await CarItems.ReplaceOneAsync(filterCarItem, new CarItem
                {
                    Id                = carItem.Id,
                    Name              = value.Name,
                    TotalRide         = value.IsTotalRideChanged ? 0 : carItem.TotalRide,
                    ChangeRide        = int.Parse(value.ChangeRide),
                    PriceOfDetail     = int.Parse(value.PriceOfDetail),
                    DateOfReplace     = DateTime.Parse(value.DateOfReplace),
                    RecomendedReplace = int.Parse(value.RecomendedReplace),
                    CarEntityId       = carItem.CarEntityId
                });

                return(true);
            }
            return(false);
        }
コード例 #3
0
        public bool IsEmptyDb()
        {
            var builder = new FilterDefinitionBuilder <CarEntity>();
            var filter  = builder.Empty; // фильтр для выборки всех документов

            return(!CarEntities.Find(filter).Any());
        }
コード例 #4
0
        public async Task <CarItemsSendModel> GetCarItemsAsync(int offset, int limit, string userId)
        {
            var car = await CarEntities.Find(x => x.UserId == userId && x.IsCurrent == true).FirstOrDefaultAsync();

            if (car != null)
            {
                var carItems = await CarItems.Find(x => x.CarEntityId == car.Id).ToListAsync();

                int carItemsCount = carItems.Count();
                if (
                    offset >= 0 &&
                    limit > 0
                    )
                {
                    var carEntitySendData = new CarItemsSendModel
                    {
                        CountCarsItems = carItemsCount,
                        CarEntityId    = car.Id,
                        CarItems       = carItemsCount == 0 || carItemsCount <= offset ? null :
                                         carItems.Skip(offset).Take(limit).Select(x => new CarItemSendModel
                        {
                            CarItemId         = x.CarItemId,
                            Name              = x.Name,
                            TotalRide         = x.TotalRide,
                            ChangeRide        = x.ChangeRide,
                            PriceOfDetail     = x.PriceOfDetail,
                            DateOfReplace     = x.DateOfReplace,
                            RecomendedReplace = x.RecomendedReplace
                        })
                    };
                    return(carEntitySendData);
                }
            }
            return(null);
        }
コード例 #5
0
        public async Task <bool> SetTotalRideAsync(UpdateTotalRideModel value, string userId)
        {
            var carEntity = await CarEntities.Find(x => x.UserId == userId && x.Id == value.Id).FirstOrDefaultAsync();

            if (carEntity != null)
            {
                if (value.TotalRide <= carEntity.CarsTotalRide)
                {
                    return(true);
                }

                if (value.TotalRide > 0)
                {
                    int carEntityTotalRide = carEntity.CarsTotalRide;

                    var filterCarEntity = Builders <CarEntity> .Filter.Eq("Id", carEntity.Id);

                    var filterCarItem = Builders <CarItem> .Filter.Eq("CarEntityId", carEntity.Id);

                    var updateCarItemTotalRide = Builders <CarItem> .Update.Inc <int>("TotalRide", (value.TotalRide - carEntityTotalRide));

                    var updateCarEntityTotalRide = Builders <CarEntity> .Update.Set("CarsTotalRide", value.TotalRide);

                    var resultCarItem = await CarItems.UpdateManyAsync(filterCarItem, updateCarItemTotalRide);

                    var resultCarEntity = await CarEntities.UpdateOneAsync(filterCarEntity, updateCarEntityTotalRide);

                    return(true);
                }
            }

            return(false);
        }
コード例 #6
0
        public JsonResult <List <Car> > GetSearchAll(string id)
        {
            CarEntities db   = new CarEntities();
            List <Car>  cars = db.Cars.Where(
                c => c.Make.Contains(id) ||
                c.Model.Contains(id) ||
                c.Color.Contains(id)
                ).ToList();

            return(Json(cars));
        }
コード例 #7
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //// This doesn't count login failures towards account lockout
            //// To enable password failures to trigger account lockout, change to shouldLockout: true
            //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            //switch (result)
            //{
            //    case SignInStatus.Success:
            //        return RedirectToLocal(returnUrl);
            //    case SignInStatus.LockedOut:
            //        return View("Lockout");
            //    case SignInStatus.RequiresVerification:
            //        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            //    case SignInStatus.Failure:
            //    default:
            //        ModelState.AddModelError("", "Invalid login attempt.");
            //        return View(model);
            //}

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true

            CarEntities context      = new CarEntities();
            var         loggedInUser = context.Users.FirstOrDefault(s => s.Email == model.Email && s.Password == model.Password);
            var         result       = SignInStatus.Failure;

            if (loggedInUser != null)
            {
                result          = SignInStatus.Success;
                Session["User"] = loggedInUser;
            }
            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
コード例 #8
0
        public async Task <bool> DeleteCarItemAsync(string detailId, string userId)
        {
            var carEntity = await CarEntities.Find(x => x.IsCurrent == true && x.UserId == userId).FirstOrDefaultAsync();

            if (carEntity != null)
            {
                var result = await CarItems.DeleteOneAsync(x => x.Id == detailId && x.CarEntityId == carEntity.Id);

                return(true);
            }
            return(false);
        }
コード例 #9
0
        public ActionResult SearchInventory(string input)
        {
            CarEntities db = new CarEntities();

            List <Car> list = db.Cars.Where(
                c => c.Make.Contains(input) ||
                c.Model.Contains(input) ||
                c.Color.Contains(input)
                ).ToList();

            return(Json(list));
        }
コード例 #10
0
        public async Task <CarTotalRideModel> GetTotalRideAsync(string userId)
        {
            var car = await CarEntities.Find(x => x.UserId == userId && x.IsCurrent == true).FirstOrDefaultAsync();

            if (car != null)
            {
                return(new CarTotalRideModel
                {
                    CarsTotalRide = car.CarsTotalRide
                });
            }
            return(null);
        }
コード例 #11
0
        public IHttpActionResult RegisterCar(Int32?customerid, CarEntities model)
        {
            try
            {
                using (var ts = new TransactionScope())
                {
                    if (model == null)
                    {
                        response.Data    = null;
                        response.Error   = true;
                        response.Message = "Error, Empty model";
                        return(Content(HttpStatusCode.BadRequest, response));
                    }
                    else
                    {
                        Car car = new Car();

                        if (!customerid.HasValue)
                        {
                            response.Data    = null;
                            response.Error   = true;
                            response.Message = "Error, customer empty";
                            return(Content(HttpStatusCode.BadRequest, response));
                        }
                        else
                        {
                            context.Car.Add(car);

                            car.BrandId     = model.BrandId;
                            car.Description = model.Description;
                            car.Placa       = model.Placa;
                            car.CustomerId  = model.CustomerId;


                            context.SaveChanges();

                            response.Data    = null;
                            response.Error   = false;
                            response.Message = "Success, saved car";
                        }

                        ts.Complete();
                    }
                }
                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(Unauthorized());
            }
        }
コード例 #12
0
        public async Task <CarEntity> GetCurrentCarAsync(string userId)
        {
            var builder = Builders <CarEntity> .Filter;
            var filter  = builder.Eq("UserId", userId) & builder.Eq("IsCurrent", true);

            var res = await CarEntities.Find(filter).FirstOrDefaultAsync();

            if (res != null)
            {
                return(res);
            }

            return(null);
        }
コード例 #13
0
        public IHttpActionResult UpdateCar(Int32?customerid, CarEntities model)
        {
            try
            {
                using (var ts = new TransactionScope())
                {
                    if (model == null)
                    {
                        response.Data    = null;
                        response.Error   = true;
                        response.Message = "Error, Empty model";
                        return(Content(HttpStatusCode.BadRequest, response));
                    }
                    else
                    {
                        Car car = new Car();


                        if (model.CarId.HasValue && customerid.HasValue)
                        {
                            car = context.Car.FirstOrDefault(x => x.CustomerId == customerid && x.CarId == model.CarId);

                            car.BrandId     = model.BrandId;
                            car.Description = model.Description;
                            car.Placa       = model.Placa;
                            car.CustomerId  = model.CustomerId;
                            context.SaveChanges();

                            response.Data    = null;
                            response.Error   = false;
                            response.Message = "Success, updated car";
                        }
                        else
                        {
                            response.Data    = null;
                            response.Error   = true;
                            response.Message = "Error, Local or provider id empty";
                        }

                        ts.Complete();
                    }
                }
                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(Unauthorized());
            }
        }
コード例 #14
0
        public async Task AddUserNewCarAsync(CarEntity carEntity)
        {
            if (carEntity.IsCurrent)
            {
                var builder = Builders <CarEntity> .Filter;

                await CarEntities.UpdateManyAsync(builder.Eq("UserId", carEntity.UserId), new BsonDocument("$set", new BsonDocument("IsCurrent", false)));
            }

            if (!await CarEntities.Find(x => x.UserId == carEntity.UserId).AnyAsync())
            {
                carEntity.IsCurrent = true;
            }


            await CarEntities.InsertOneAsync(carEntity);
        }
コード例 #15
0
        public async Task <IList <CarItem> > FindCarItem(string name, string userId)
        {
            var car = await CarEntities.Find(x => x.UserId == userId && x.IsCurrent == true).FirstOrDefaultAsync();

            if (car != null)
            {
                var builder = new FilterDefinitionBuilder <CarItem>();
                var filter  = builder.Empty;          // фильтр для выборки всех документов

                if (!String.IsNullOrWhiteSpace(name)) // фильтр по имени
                {
                    filter = filter & builder.Regex("Name", new BsonRegularExpression(name)) & builder.Eq("CarEntityId", car.Id);
                }

                return(await CarItems.Find(filter).ToListAsync());
            }
            return(null);
        }
コード例 #16
0
        public async Task <bool> AddNewCarItemAsync(CarItem data, string userId)
        {
            var car = await CarEntities.Find(x => x.UserId == userId && x.Id == data.CarEntityId).FirstOrDefaultAsync();

            if (car != null)
            {
                //await this.SetTotalRideAsync(new UpdateTotalRideModel
                //{
                //    Id = data.CarEntityId,
                //    TotalRide = car.CarsTotalRide
                //}, userId);

                await CarItems.InsertOneAsync(data);

                return(true);
            }

            return(false);
        }
コード例 #17
0
        public async Task <bool> DeleteUserCarAsync(string carEntityId, string userId)
        {
            var car = await CarEntities.Find(x => x.Id == carEntityId && x.UserId == userId).FirstOrDefaultAsync();

            if (car != null)
            {
                if (car.IsCurrent)
                {
                    await SetUserCurCarAsync(CarEntities.Find(x => x.Id != carEntityId && x.UserId == userId).FirstOrDefault().Id, userId);
                }

                var result = await CarEntities.DeleteOneAsync(x => x.Id == carEntityId && x.UserId == userId);

                await CarItems.DeleteManyAsync(x => x.CarEntityId == carEntityId);

                return(true);
            }

            return(false);
        }
コード例 #18
0
        public async Task <bool> SetUserCurCarAsync(string carEntityId, string userId)
        {
            if (carEntityId != null && userId != null)
            {
                var builder = Builders <CarEntity> .Filter;
                var filter  = builder.Eq("UserId", userId) & builder.Eq("Id", carEntityId);

                await CarEntities.UpdateManyAsync(builder.Eq("UserId", userId), new BsonDocument("$set", new BsonDocument("IsCurrent", false)));

                var result = await CarEntities.UpdateOneAsync(
                    filter,
                    new BsonDocument("$set", new BsonDocument("IsCurrent", true)));

                if (result.ModifiedCount > 0)
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #19
0
        private static void CreateUser(CarEntities context, string email, string role, string firstName, string lastName)
        {
            if (!context.Users.Any(s => s.Email == email))
            {
                var user = new Models.User
                {
                    Email     = email,
                    FirstName = firstName,
                    LastName  = lastName,
                    Password  = "******",
                    Role      = role
                };
                Salesperson sales = new Salesperson();
                sales.User = user;

                context.Users.Add(user);
                context.Salespersons.Add(sales);
                context.SaveChanges();
            }
        }
コード例 #20
0
        public async Task <bool> UpdateUserCarAsync(EditCarModel carEntity, string userId)
        {
            var builderFilter = Builders <CarEntity> .Filter;
            var builderUpdate = Builders <CarEntity> .Update;

            var filterUser      = builderFilter.Eq("UserId", userId);
            var filterCarEntity = builderFilter.Eq("Id", carEntity.Id);
            var update          = builderUpdate.Set("CarEntityName", carEntity.CarEntityName);

            var car = await CarEntities.Find(filterUser& filterCarEntity).FirstOrDefaultAsync();

            if (car != null)
            {
                await this.SetTotalRideAsync(new UpdateTotalRideModel
                {
                    Id        = car.Id,
                    TotalRide = carEntity.CarsTotalRide
                }, userId);

                await CarEntities.UpdateOneAsync(filterUser& filterCarEntity, update);

                if (carEntity.IsCurrent &&
                    !(car.IsCurrent))
                {
                    await SetUserCurCarAsync(carEntity.Id, userId);
                }

                if (!carEntity.IsCurrent &&
                    car.IsCurrent)
                {
                    await SetUserCurCarAsync(CarEntities.Find(filterUser & !filterCarEntity).FirstOrDefault().Id, userId);
                }
                return(true);
            }
            return(false);
        }
コード例 #21
0
 public LookupRepository()
 {
     db = new CarEntities();
 }
コード例 #22
0
 public async Task <List <CarEntity> > GetAllUsersCarsAsync(string userId)
 {
     // return await CarEntities.Find(x => x.UserId == userId).ToListAsync();
     return(await CarEntities.Find(new BsonDocument("UserId", userId)).ToListAsync());
 }
コード例 #23
0
 public UserRepository()
 {
     db = new CarEntities();
 }
コード例 #24
0
 public SalesPersonRepository()
 {
     db = new CarEntities();
 }
コード例 #25
0
 public VehicleRepository()
 {
     db = new CarEntities();
 }
コード例 #26
0
 public ReportRepository()
 {
     db = new CarEntities();
 }
コード例 #27
0
 public SpecialsRepository()
 {
     db = new CarEntities();
 }