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