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); }
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); }
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); }