public List<BuildingPrimitive> GetBuildings(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<Building> result = (string.IsNullOrWhiteSpace(filter)) ? (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Buildings.ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Buildings.Where(x => !x.Deleted.HasValue).ToList() : ctx.Buildings.Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue).ToList() : (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Buildings.Where(x => (x.InternalName.Contains(filter) || x.Name.Contains(filter))).ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Buildings.Where(x => !x.Deleted.HasValue && (x.InternalName.Contains(filter) || x.Name.Contains(filter))).ToList() : ctx.Buildings.Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue && (x.InternalName.Contains(filter) || x.Name.Contains(filter))).ToList(); return result.Select(x => x.GetPrimitive()).ToList(); ; } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public void DeactiveOrder(OrderPrimitive orderPrimitive) { try { using (SmartWorkingEntities context = new SmartWorkingEntities()) { Order order = orderPrimitive.GetEntity(); Order existingObject = context.Orders.Where(x => x.Id == order.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null) { throw new Exception("Only exists delivery note can be canceled."); } order.Deactivated = DateTime.Now; context.Orders.ApplyCurrentValues(order); context.SaveChanges(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public static bool ValidateForNewEntity(this Car toValidate, List<ValidationResult> validationResults, SmartWorkingEntities context) { bool result = true; var conflicted = context.Cars.Where( x => x.InternalName.Trim().ToUpper() == toValidate.InternalName.Trim().ToUpper() || x.RegistrationNumber.Replace(" ", string.Empty).ToUpper() == toValidate.RegistrationNumber.Replace(" ", string.Empty).ToUpper()). FirstOrDefault(); if (conflicted != null) { result = false; if (validationResults == null) validationResults = new List<ValidationResult>(); if (conflicted.InternalName.Trim().ToUpper() == toValidate.InternalName.Trim().ToUpper()) { validationResults.Add(new ValidationResult("Samochód o podanym symbolu już istnieje.")); } if (conflicted.RegistrationNumber.Replace(" ", string.Empty).ToUpper() == toValidate.RegistrationNumber.Replace(" ", string.Empty).ToUpper()) { validationResults.Add(new ValidationResult("Samochód o podanym numerze rejestracyjnym już istnieje.")); } } return result; }
/// <summary> /// Deletes the <see cref="DeliveryNote"/>. /// </summary> /// <param name="deliveryNote">The delivery note which will be canceled.</param> public void ActiveDeliveryNote(DeliveryNotePrimitive deliveryNotePrimitive) { try { using (SmartWorkingEntities context = new SmartWorkingEntities()) { DeliveryNote deliveryNote = deliveryNotePrimitive.GetEntity(); DeliveryNote existingObject = context.DeliveryNotes.Where(x => x.Id == deliveryNote.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null) { throw new Exception("Only exists delivery note can be canceled."); } deliveryNote.DeactivationReason = string.Empty; deliveryNote.Deactivated = null; context.DeliveryNotes.ApplyCurrentValues(deliveryNote); context.SaveChanges(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Updates the contractor. /// </summary> /// <param name="contractorPrimitive">The contractor primitive.</param> public void CreateOrUpdateContractor(ContractorPrimitive contractorPrimitive) { try { using (SmartWorkingEntities context = new SmartWorkingEntities()) { Contractor contractor = contractorPrimitive.GetEntity(); Contractor existingObject = context.Contractors.Where(x => x.Id == contractor.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && contractor.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } //Item has no PK value, must be new else if (contractor.Id <= 0) { context.Contractors.AddObject(contractor); } //Item was retrieved, and the item passed has a valid ID, do an update else { context.Contractors.ApplyCurrentValues(contractor); } context.SaveChanges(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Updates the car.oo /// </summary> /// <param name="carPrimitive">The car primitive.</param> public void CreateOrUpdateCar(CarPrimitive carPrimitive) { try { List<ValidationResult> validationResults = carPrimitive.ValidateClientSide(); if (validationResults != null && validationResults.Count > 0) { throw new FaultException<List<ValidationResult>>(validationResults, "Walidacja obiektu się nie powiodła."); } using (SmartWorkingEntities context = new SmartWorkingEntities()) { Car car = carPrimitive.GetEntity(); Car existingObject = context.Cars.Where(x => x.Id == car.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && car.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } //Item has no PK value, must be new else if (car.Id <= 0) { if (car.ValidateForNewEntity(validationResults, context)) { context.Cars.AddObject(car); } } //Item was retrieved, and the item passed has a valid ID, do an update else { if (car.ValidateForExistingEntity(validationResults, context)) { context.Cars.ApplyCurrentValues(car); } } if (validationResults != null && validationResults.Count > 0) { throw new FaultException<List<ValidationResult>>(validationResults, "Walidacja obiektu się nie powiodła."); } context.SaveChanges(); } } catch(FaultException<List<ValidationResult>>) { throw; } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Updates the <see cref="DeliveryNote"/>. /// </summary> /// <param name="deliveryNote">The delivery note which will be updated.</param> public DeliveryNotePrimitive CreateOrUpdateDeliveryNote(DeliveryNotePrimitive deliveryNotePrimitive) { try { using (SmartWorkingEntities context = new SmartWorkingEntities()) { DeliveryNote deliveryNote = deliveryNotePrimitive.GetEntity(); DeliveryNote existingObject = context.DeliveryNotes.Where(x => x.Id == deliveryNote.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && deliveryNote.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } DeliveryNote checkingNumber = context.DeliveryNotes.Where(x => x.Number == deliveryNote.Number && x.Year == deliveryNote.Year). FirstOrDefault(); //Item has no PK value, must be new);) if (deliveryNote.Id <= 0) { deliveryNote.DateDrawing = DateTime.Now; //deliveryNote.Number = GetNextDeliveryNumber(); if (checkingNumber != null) { throw new Exception("Ten numer WZ'tki juz istnieje!"); } context.DeliveryNotes.AddObject(deliveryNote); } //Item was retrieved, and the item passed has a valid ID, do an update else { if (checkingNumber != null && checkingNumber.Id != deliveryNote.Id) { throw new Exception("Ten numer WZ'tki juz istnieje!"); } context.DeliveryNotes.ApplyCurrentValues(deliveryNote); } context.SaveChanges(); return deliveryNote; } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public void DeleteBuilding(BuildingPrimitive buildingPrimitive) { try { using (SmartWorkingEntities context = new SmartWorkingEntities()) { Building building = context.Buildings.Where(x => x.Id == buildingPrimitive.Id).FirstOrDefault(); if (building != null) { building.Deleted = DateTime.Now; context.SaveChanges(); } else { throw new Exception("This car does not exist in db."); } } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Deletes the material. /// </summary> /// <param name="material">The material which will be deleted.</param> public void DeleteMaterial(MaterialPrimitive materialPrimitive) { try { using (var context = new SmartWorkingEntities()) { Material material = context.Materials.Where(x => x.Id == materialPrimitive.Id).FirstOrDefault(); if (material != null) { material.Deleted = DateTime.Now; context.SaveChanges(); } else { throw new Exception("This car does not exist in db."); } } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Deletes the contractor. /// </summary> /// <param name="contractorPrimitive">The contractor primitive.</param> public void DeleteContractor(ContractorPrimitive contractorPrimitive) { try { //TODO: if is not used in any DeliveryNotes than delete. using (SmartWorkingEntities context = new SmartWorkingEntities()) { Contractor contractor = context.Contractors.Where(x => x.Id == contractorPrimitive.Id).FirstOrDefault(); if (contractor != null) { contractor.Deleted = DateTime.Now; context.SaveChanges(); } else { throw new Exception("This contractor does not exist in db."); } } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public void CreateOrUpdateUserAndRolesPackage(UserAndRolesPackage userPackage) { try { if (userPackage != null && userPackage.User != null) { using (SmartWorkingEntities context = new SmartWorkingEntities()) { User existingObject = context.Users.Include("Roles").Where(x => x.Id == userPackage.User.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && userPackage.User.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } //Item has no PK value, must be new //Item has no PK value, must be new); if (userPackage.User.Id <= 0) { User user = userPackage.User.GetEntity(); user.PasswordSalz = GenerateSalt(); user.Password = EncodePassword(user.Password, user.PasswordSalz); context.Users.AddObject(user); foreach (RolePrimitive rolePrimitive in userPackage.Roles) { Role role = rolePrimitive.GetEntity(); role.Users.Add(user); } } //Item was retrieved, and the item passed has a valid ID, do an update else { List<RolePrimitive> existingElements = existingObject.Roles.Select(x => x.GetPrimitive()).ToList(); List<RolePrimitive> newElements = userPackage.Roles.ToList(); List<RolePrimitive> theSameElements = newElements.Where(x => existingElements.Select(y => y.Id).Contains(x.Id)).ToList(); existingElements.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id)); newElements.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id)); //remove if (existingElements.Count() > 0) { List<int> existingRecipeComponentIds = existingElements.Select(x => x.Id).ToList(); List<RolePrimitive> recipeComponentListToDelete = context.Roles.Where(x => existingRecipeComponentIds.Contains(x.Id)).Select(y => y.GetPrimitive()).ToList(); foreach (RolePrimitive recipeComponent in recipeComponentListToDelete) { context.Roles.DeleteObject(recipeComponent.GetEntity()); } } //add foreach (RolePrimitive newRecipeComponent in newElements) { context.Roles.AddObject(newRecipeComponent.GetEntity()); } //if is empty don't change password if (string.IsNullOrEmpty(userPackage.User.Password)) { userPackage.User.Password = existingObject.Password; } else { userPackage.User.Password = EncodePassword(userPackage.User.Password, existingObject.PasswordSalz); } context.Users.ApplyCurrentValues(userPackage.User.GetEntity()); } context.SaveChanges(); } } else { throw new Exception("str_Inpute parameter was wrong."); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public List<OrderPackage> GetOrderPackageList(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<Order> result = (string.IsNullOrWhiteSpace(filter)) ? (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Orders.Include("DeliveryNotes.Car").Include("DeliveryNotes.Driver").Include("Recipe").Include("ClientBuilding.Client").Include("ClientBuilding.Building").ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Orders.Include("DeliveryNotes.Car").Include("DeliveryNotes.Driver").Include("Recipe").Include("ClientBuilding.Client").Include("ClientBuilding.Building").Where(x => !x.Deleted.HasValue).ToList() : ctx.Orders.Include("DeliveryNotes.Car").Include("DeliveryNotes.Driver").Include("Recipe").Include("ClientBuilding.Client").Include("ClientBuilding.Building").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue).ToList() : (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Orders.Include("DeliveryNotes.Car").Include("DeliveryNotes.Driver").Include("Recipe").Include("ClientBuilding.Client").Include("ClientBuilding.Building").Where(x => x.ClientBuilding != null && x.ClientBuilding.Client != null && x.ClientBuilding.Client.Name.Contains(filter)).ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Orders.Include("DeliveryNotes.Car").Include("DeliveryNotes.Driver").Include("Recipe").Include("ClientBuilding.Client").Include("ClientBuilding.Building").Where(x => !x.Deleted.HasValue && x.ClientBuilding != null && x.ClientBuilding.Client != null && x.ClientBuilding.Client.Name.Contains(filter)).ToList() : ctx.Orders.Include("DeliveryNotes.Car").Include("DeliveryNotes.Driver").Include("Recipe").Include("ClientBuilding.Client").Include("ClientBuilding.Building").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue && x.ClientBuilding != null && x.ClientBuilding.Client != null && x.ClientBuilding.Client.Name.Contains(filter)).ToList(); return result.Select(x => x.GetOrderPackage()).ToList(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public int GetNextDeliveryNumber() { try { using (var ctx = new SmartWorkingEntities()) { int? maxDeliveryNumberRead = ctx.DeliveryNotes.Where(x => x.Year == DateTime.Now.Year).Max(x => x.Number); return maxDeliveryNumberRead.HasValue ? maxDeliveryNumberRead.Value + 1 : 1; } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
private List<DeliveryNoteReportPackage> GetDeliveryNoteReportPackageListData(DateTime? startTime, DateTime? endTime) { try { using (var ctx = new SmartWorkingEntities()) { List<DeliveryNoteReportPackage> result = new List<DeliveryNoteReportPackage>(); IQueryable<DeliveryNote> allDeliveryNotesQuery = ctx.DeliveryNotes.Include("Car").Include("Driver").Include("Order.Recipe").Include( "Order.ClientBuilding.Building").Include("Order.ClientBuilding.Client"); if (startTime.HasValue) allDeliveryNotesQuery = allDeliveryNotesQuery.Where(x => x.DateDrawing >= startTime.Value); if (endTime.HasValue) allDeliveryNotesQuery = allDeliveryNotesQuery.Where(x => x.DateDrawing <= endTime.Value); List<DeliveryNote> allDeliveryNotes = allDeliveryNotesQuery.ToList(); foreach (DeliveryNote deliveryNote in allDeliveryNotes) { result.Add(deliveryNote.GetDeliveryNoteReportPackage()); } return result; } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Gets the <see cref="DeliveryNote"/> filtered be <paramref name="filter"/> and <paramref name="showCanceledDeliveryNotes"/>. /// </summary> /// <param name="filter">Used to filtering result. Loaded <see cref="DeliveryNote"/> object will contain this string.</param> /// <param name="showCanceledDeliveryNotes">if set to <c>true</c> then loaded <see cref="DeliveryNote"/> object will contain <see cref="DeliveryNote"/> which are deactivated; otherwise not.</param> /// <returns> /// List of <see cref="DeliveryNote"/> filtered by <paramref name="filter"/> and <paramref name="showCanceledDeliveryNotes"/>. /// </returns> public List<DeliveryNotePrimitive> GetDeliveryNotes(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<DeliveryNote> result; //if (string.IsNullOrWhiteSpace(filter)) //{ // if (showCanceledDeliveryNotes) // { // result = ctx.DeliveryNotes.Include("Building.Contractor").Include("Recipe").Include("Driver").Include("Car").ToList(); // } // else // { // result = ctx.DeliveryNotes.Include("Building.Contractor").Include("Recipe").Include("Driver").Include("Car").Where(x => x.Canceled != DateTime.MinValue).ToList(); // } //} //else //{ // if (showCanceledDeliveryNotes) // { // result = ctx.DeliveryNotes.Include("Building.Contractor").Include("Recipe").Include("Driver").Include("Car").Where(x => (x.Building.City + " " + x.Building.Street).Contains(filter)).ToList(); // } // else // { // result = ctx.DeliveryNotes.Include("Building.Contractor").Include("Recipe").Include("Driver").Include("Car").Where(x => x.Canceled != DateTime.MinValue && (x.Building.City + " " + x.Building.Street).Contains(filter)).ToList(); // } //} //return result.Select(x => x.GetPrimitive()).ToList(); return null; } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public List<CarAndDriverPackage> GetCarAndDriverPackageList(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<Car> result = (string.IsNullOrWhiteSpace(filter)) ? (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Cars.Include("Driver").ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Cars.Include("Driver").Where(x => !x.Deleted.HasValue).ToList() : ctx.Cars.Include("Driver").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue).ToList() : (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Cars.Include("Driver").Where(x => (x.InternalName.Contains(filter) || x.RegistrationNumber.Contains(filter))).ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Cars.Include("Driver").Where(x => !x.Deleted.HasValue && (x.InternalName.Contains(filter) || x.RegistrationNumber.Contains(filter))).ToList() : ctx.Cars.Include("Driver").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue && (x.InternalName.Contains(filter) || x.RegistrationNumber.Contains(filter))).ToList(); return result.Select(x => x.GetCarAndDriverPackage()).ToList(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public DriversCarsReportPackage GetDriversCarsDataReport(DateTime startTime, DateTime endTime) { try { using (var ctx = new SmartWorkingEntities()) { DriversCarsReportPackage result = new DriversCarsReportPackage(); List<DeliveryNote> allDeliveryNotes = ctx.DeliveryNotes.Include("Car").Include("Driver").Where(x => !x.Deactivated.HasValue && x.DateDrawing >= startTime && x.DateDrawing <= endTime).ToList(); foreach (DeliveryNote deliveryNote in allDeliveryNotes) { if (deliveryNote.Car != null) { if (result.ColumntElements.Where(x => x.Id == deliveryNote.Car.Id).FirstOrDefault() == null) { result.ColumntElements.Add(deliveryNote.Car.GetPrimitive()); } } if (deliveryNote.Driver != null) { if (result.RowElements.Where(x => x.Id == deliveryNote.Driver.Id).FirstOrDefault() == null) { result.RowElements.Add(deliveryNote.Driver.GetPrimitive()); } } ReportRow row = result.RowValues.Where(x => x.Id == deliveryNote.Driver_Id).FirstOrDefault(); if (row == null) { row = new ReportRow(); row.Id = deliveryNote.Driver_Id.Value; row.ReportValues.Add(new ReportValue() {Id = deliveryNote.Car_Id.Value, Amount = deliveryNote.Amount, NoOfTransports = 1}); result.RowValues.Add(row); } else { ReportValue reportValue = row.ReportValues.Where(x => x.Id == deliveryNote.Car_Id.Value).FirstOrDefault(); if (reportValue == null) { reportValue = new ReportValue(); reportValue.Id = deliveryNote.Car_Id.Value; reportValue.Amount = deliveryNote.Amount; reportValue.NoOfTransports = 1; row.ReportValues.Add(reportValue); } else { reportValue.Amount += deliveryNote.Amount; reportValue.NoOfTransports += 1; } } } return result; } } catch(Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Gets the recipes filtered be <paramref name="recipesFilter"/>. /// </summary> /// <param name="recipesFilter">The recipes filter.</param> /// <returns> /// List of Recipe filtered by <paramref name="recipesFilter"/>. Recipe contains list of Material contains to this Recipe. /// </returns> public List<RecipePrimitive> GetRecipes(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<Recipe> result = (string.IsNullOrWhiteSpace(filter)) ? (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Recipes.ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Recipes.Where(x => !x.Deleted.HasValue).ToList() : ctx.Recipes.Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue).ToList() : (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Recipes.Where(x => (x.Code.StartsWith(filter))).ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Recipes.Where(x => !x.Deleted.HasValue && (x.Code.StartsWith(filter))).ToList() : ctx.Recipes.Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue && (x.Code.StartsWith(filter))).ToList(); return result.Select(x => x.GetPrimitive()).ToList(); //todo po symbolu cemnetu //ctx.Recipes.Where(x => (x.Code.Contains(filter) || // x.RecipeComponents.Where( // y => ((y.Material.MaterialType.HasValue && y.Material.MaterialType.Value == (int)MaterialTypeEnum.Concrete) && // (y.Material.InternalName.Contains(filter)))).Count() > 0 // )).ToList() } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public void UndeleteRecipe(RecipePrimitive recipePrimitive) { try { if (recipePrimitive != null) { using (SmartWorkingEntities context = new SmartWorkingEntities()) { Recipe recipe = context.Recipes.Where(x => x.Id == recipePrimitive.Id).FirstOrDefault(); if (recipe != null) { recipe.Deleted = null; context.SaveChanges(); } else { throw new Exception("This car does not exist in db."); } context.SaveChanges(); } } else { throw new Exception("str_Inpute parameter was wrong."); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Gets the <see cref="RecipePackage"/> list filtered by <paramref name="filter"/>. /// </summary> /// <param name="filter">The contractor name filter.</param> /// <returns> /// List of contractors filtered by <paramref name="filter"/>. /// </returns> public List<RecipePackage> GetRecipePackageList(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<Recipe> result = (string.IsNullOrWhiteSpace(filter)) ? (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Recipes.Include("RecipeComponents.Material.Producer").Include("RecipeComponents.Material.Deliverer").ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Recipes.Include("RecipeComponents.Material.Producer").Include("RecipeComponents.Material.Deliverer").Where(x => !x.Deleted.HasValue).ToList() : ctx.Recipes.Include("RecipeComponents.Material.Producer").Include("RecipeComponents.Material.Deliverer").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue).ToList() : (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Recipes.Include("RecipeComponents.Material.Producer").Include("RecipeComponents.Material.Deliverer").Where(x => (x.Code.StartsWith(filter))).ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Recipes.Include("RecipeComponents.Material.Producer").Include("RecipeComponents.Material.Deliverer").Where(x => !x.Deleted.HasValue && (x.Code.StartsWith(filter))).ToList() : ctx.Recipes.Include("RecipeComponents.Material.Producer").Include("RecipeComponents.Material.Deliverer").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue && (x.Code.StartsWith(filter))).ToList(); return result.Select(x => x.GetRecipesPackage()).ToList(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public void CreateOrUpdateRecipePackage(RecipePackage recipePackage) { try { if (recipePackage != null && recipePackage.Recipe != null) { using (SmartWorkingEntities context = new SmartWorkingEntities()) { Recipe existingObject = context.Recipes.Include("RecipeComponents").Where(x => x.Id == recipePackage.Recipe.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && recipePackage.Recipe.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } //Item has no PK value, must be new //Item has no PK value, must be new); if (recipePackage.Recipe.Id <= 0) { Recipe recipe = recipePackage.Recipe.GetEntity(); context.Recipes.AddObject(recipe); foreach (RecipeComponentAndMaterialPackage recipeComponentAndMaterialPackage in recipePackage.RecipeComponentAndMaterialList) { RecipeComponentPrimitive recipeComponentPrimitive = recipeComponentAndMaterialPackage.GetRecipeComponentPrimitiveWithReference(); if (recipeComponentPrimitive != null) { recipeComponentPrimitive.Id = 0; RecipeComponent recipeComponent= recipeComponentPrimitive.GetEntity(); recipeComponent.Recipe = recipe; context.RecipeComponents.AddObject(recipeComponent); } } } //Item was retrieved, and the item passed has a valid ID, do an update else { List<RecipeComponentPrimitive> existingRecipeComponents = existingObject.RecipeComponents.Select(x => x.GetPrimitive()).ToList(); List<RecipeComponentPrimitive> newRecipeComponents = recipePackage.GetRecipeComponentListWithReference().ToList(); List<RecipeComponentPrimitive> theSameElements = newRecipeComponents.Where(x => existingRecipeComponents.Select(y => y.Id).Contains(x.Id)).ToList(); existingRecipeComponents.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id)); newRecipeComponents.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id)); //remove if (existingRecipeComponents.Count() > 0) { List<int> existingRecipeComponentIds = existingRecipeComponents.Select(x => x.Id).ToList(); List<RecipeComponent> recipeComponentListToDelete = context.RecipeComponents.Where(x => existingRecipeComponentIds.Contains(x.Id)).ToList(); foreach (RecipeComponent recipeComponent in recipeComponentListToDelete) { context.RecipeComponents.DeleteObject(recipeComponent); } } //add foreach (RecipeComponentPrimitive newRecipeComponent in newRecipeComponents) { context.RecipeComponents.AddObject(newRecipeComponent.GetEntity()); } context.Recipes.ApplyCurrentValues(recipePackage.Recipe.GetEntity()); } context.SaveChanges(); //TODO: FOR THE FUTURE //if (recipePackage.Recipe.Id > 0) //{ // existingObject.Deleted = DateTime.Now; // context.Recipes.ApplyCurrentValues(existingObject); //} //Recipe recipe = recipePackage.Recipe.GetEntity(); //recipe.Id = 0; //context.Recipes.AddObject(recipe); //foreach ( // RecipeComponentAndMaterialPackage recipeComponentAndMaterialPackage in // recipePackage.RecipeComponentAndMaterialList) //{ // recipeComponentAndMaterialPackage.RecipeComponent.Id = 0; // RecipeComponent recipeComponent = // recipeComponentAndMaterialPackage.GetRecipeComponentPrimitiveWithReference().GetEntity(); // recipeComponent.Recipe = recipe; // context.RecipeComponents.AddObject(recipeComponent); //} //context.SaveChanges(); } } else { throw new Exception("str_Inpute parameter was wrong."); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Gets the <see cref="ClientAndClientBuildingsPackage"/> list filtered by <paramref name="filter"/>. /// </summary> /// <param name="filter">The client name filter.</param> /// <returns> /// List of clients filtered by <paramref name="filter"/>. /// </returns> public List<ClientAndClientBuildingsPackage> GetClientAndBuildingsPackageList(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<Client> result = (string.IsNullOrWhiteSpace(filter)) ? (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Clients.ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Clients.Where(x => !x.Deleted.HasValue).ToList() : ctx.Clients.Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue).ToList() : (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Clients.Where(x => (x.InternalName.Contains(filter) || x.Name.Contains(filter))).ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Clients.Where(x => !x.Deleted.HasValue && (x.InternalName.Contains(filter) || x.Name.Contains(filter))).ToList() : ctx.Clients.Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue && (x.InternalName.Contains(filter) || x.Name.Contains(filter))).ToList(); List<int> clientIds = result.Select(x => x.Id).ToList(); List<ClientBuilding> clientBuildings = ctx.ClientBuildings.Include("Building").Where(cb => clientIds.Contains(cb.Client.Id) && !cb.Deleted.HasValue && cb.Building != null && !cb.Building.Deleted.HasValue).ToList(); return result.Select(x => x.GetClientAndBuildingsPackage()).ToList(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public void UndeleteClient(ClientPrimitive clientPrimitive) { try { //TODO: if is not used in any DeliveryNotes than delete. using (SmartWorkingEntities context = new SmartWorkingEntities()) { Client car = context.Clients.Where(x => x.Id == clientPrimitive.Id).FirstOrDefault(); if (car != null) { car.Deleted = null; context.SaveChanges(); } else { throw new Exception("This car does not exist in db."); } } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public UserAndRolesPackage ValidateUser(string userName, string password) { try { if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { using (SmartWorkingEntities context = new SmartWorkingEntities()) { User existingObject = context.Users.Include("Roles").Where(x => x.Name == userName).FirstOrDefault(); if (existingObject != null) { string passwordToCheck = EncodePassword(password, existingObject.PasswordSalz); string passwordInDb = existingObject.Password; if (passwordInDb == passwordToCheck) { return existingObject.GetUserAndRolesPackage(); } } } } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } return null; }
public List<UserAndRolesPackage> GetUserAndRolesPackageList(string filter, ListItemsFilterValues listItemsFilterValue) { try { using (var ctx = new SmartWorkingEntities()) { List<User> result = (string.IsNullOrWhiteSpace(filter)) ? (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Users.Include("Roles").ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Users.Include("Roles").Where(x => !x.Deleted.HasValue).ToList() : ctx.Users.Include("Roles").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue).ToList() : (listItemsFilterValue == ListItemsFilterValues.All) ? ctx.Users.Include("Roles").Where(x => (x.Name.StartsWith(filter))).ToList() : (listItemsFilterValue == ListItemsFilterValues.IncludeDeactive) ? ctx.Users.Include("Roles").Where(x => !x.Deleted.HasValue && (x.Name.StartsWith(filter))).ToList() : ctx.Users.Include("Roles").Where(x => !x.Deleted.HasValue && !x.Deactivated.HasValue && (x.Name.StartsWith(filter))).ToList(); List<UserAndRolesPackage> toReturn = result.Select(x => x.GetUserAndRolesPackage()).ToList(); foreach(UserAndRolesPackage userAndRolesPackage in toReturn) { if (userAndRolesPackage != null) { userAndRolesPackage.PasswordConfirm = string.Empty; userAndRolesPackage.User.Password = string.Empty; userAndRolesPackage.User.PasswordSalz = string.Empty; } } return toReturn; } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
/// <summary> /// Updates the client. /// </summary> /// <param name="clientPrimitive">The client primitive.</param> public void CreateOrUpdateClient(ClientAndClientBuildingsPackage clientAndBuildingsPackage) { try { using (SmartWorkingEntities context = new SmartWorkingEntities()) { Client client = clientAndBuildingsPackage.Client.GetEntity(); Client existingObject = context.Clients.Include("ClientBuildings").Where(x => x.Id == client.Id).FirstOrDefault(); //no record of this item in the DB, item being passed in has a PK if (existingObject == null && client.Id > 0) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")), "Obiekt nie istniał w bazie, a jego Id jest większe od 0."); } //Item has no PK value, must be new if (client.Id <= 0) { context.Clients.AddObject(client); foreach (ClientBuildingAndBuildingPackage clientBuildingPackage in clientAndBuildingsPackage.ClientBuildings) { ClientBuildingPrimitive clientBuildingPrimitive = clientBuildingPackage.GetClientBuildingPrimitiveWithReference(); if (clientBuildingPrimitive != null) { clientBuildingPrimitive.Id = 0; ClientBuilding clientBuilding = clientBuildingPrimitive.GetEntity(); clientBuilding.Client = client; context.ClientBuildings.AddObject(clientBuilding); } } } //Item was retrieved, and the item passed has a valid ID, do an update else { List<ClientBuildingPrimitive> existingClientBuildings = existingObject.ClientBuildings.Where(x => !x.IsDeleted).Select(x => x.GetPrimitive()).ToList(); List<ClientBuildingPrimitive> newClientBuildings = clientAndBuildingsPackage.GetClientBuildingListWithReference().ToList(); List<ClientBuildingPrimitive> theSameElements = newClientBuildings.Where(x => existingClientBuildings.Select(y => y.Building_Id).Contains(x.Building_Id)).ToList(); existingClientBuildings.RemoveAll(x => theSameElements.Select(y => y.Building_Id).Contains(x.Building_Id)); newClientBuildings.RemoveAll(x => theSameElements.Select(y => y.Building_Id).Contains(x.Building_Id)); //remove if (existingClientBuildings.Count() > 0) { List<int> existingClientBuildingIds = existingClientBuildings.Select(x => x.Id).ToList(); List<ClientBuilding> clientBuildingListToDelete = context.ClientBuildings.Where(x => existingClientBuildingIds.Contains(x.Id)).ToList(); foreach (ClientBuilding clientBuldingToDelete in clientBuildingListToDelete) { clientBuldingToDelete.Deleted = DateTime.Now; //context.ClientBuildings.DeleteObject(clientBuldingToDelete); } } //add foreach (ClientBuildingPrimitive clientBuildingPrimitive in newClientBuildings) { context.ClientBuildings.AddObject(clientBuildingPrimitive.GetEntity()); } context.Clients.ApplyCurrentValues(client); } context.SaveChanges(); } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public RecipePackage GetRecipePackage(int recipeId) { try { using (var ctx = new SmartWorkingEntities()) { Recipe result = ctx.Recipes.Include("RecipeComponents.Material.Producer").Include("RecipeComponents.Material.Deliverer"). Where(x => x.Id == recipeId).FirstOrDefault(); return (result != null) ? result.GetRecipesPackage() : null; } } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }
public DBBackUpPackage GetBackUpData() { try { DBBackUpPackage result = new DBBackUpPackage(); using (var ctx = new SmartWorkingEntities()) { foreach (var entity in ctx.Buildings.AsEnumerable()) { result.BuildingList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.Cars.AsEnumerable()) { result.CarList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.ClientBuildings.AsEnumerable()) { result.ClientBuildingList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.Clients.AsEnumerable()) { result.ClientList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.Contractors.AsEnumerable()) { result.ContractorList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.DeliveryNotes.AsEnumerable()) { result.DeliveryNoteList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.Drivers.AsEnumerable()) { result.DriverList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.Materials.AsEnumerable()) { result.MaterialList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.MaterialStocks.AsEnumerable()) { result.MaterialStockList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.Orders.AsEnumerable()) { result.OrderList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.RecipeComponents.AsEnumerable()) { result.RecipeComponentList.Add(entity.GetPrimitive()); } foreach (var entity in ctx.Recipes.AsEnumerable()) { result.RecipeList.Add(entity.GetPrimitive()); } } return result; } catch (Exception e) { throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message); } }