public bool DeleteDetail(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryGoodsProcedureEntities context = CreateContext()) { var dbItem = context.FactoryGoodsProcedureDetail.Find(id); if (dbItem != null) { context.FactoryGoodsProcedureDetail.Remove(dbItem); context.SaveChanges(); } return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }
public int EditDetail(int factoryGoodsProcedureID, object factoryGoodsProcedureDetail, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.FactoryGoodsProcedureDetailDTO dtoFactoryGoodsProcedureDetail = ((Newtonsoft.Json.Linq.JObject)factoryGoodsProcedureDetail).ToObject <DTO.FactoryGoodsProcedureDetailDTO>(); try { if (!dtoFactoryGoodsProcedureDetail.FactoryStepID.HasValue) { throw new Exception("You have select step."); } using (FactoryGoodsProcedureEntities context = CreateContext()) { FactoryGoodsProcedureDetail dbFactoryGoodsProcedureDetail; if (dtoFactoryGoodsProcedureDetail.FactoryGoodsProcedureDetailID > 0) { dbFactoryGoodsProcedureDetail = context.FactoryGoodsProcedureDetail.Find(dtoFactoryGoodsProcedureDetail.FactoryGoodsProcedureDetailID); } else { dbFactoryGoodsProcedureDetail = new FactoryGoodsProcedureDetail(); dbFactoryGoodsProcedureDetail.FactoryGoodsProcedureID = factoryGoodsProcedureID; context.FactoryGoodsProcedureDetail.Add(dbFactoryGoodsProcedureDetail); } if (dbFactoryGoodsProcedureDetail != null) { dbFactoryGoodsProcedureDetail.FactoryStepID = dtoFactoryGoodsProcedureDetail.FactoryStepID; dbFactoryGoodsProcedureDetail.StepIndex = dtoFactoryGoodsProcedureDetail.StepIndex; } else { throw new Exception("Could not find data. You have to save data before update component detail."); } context.SaveChanges(); return(dbFactoryGoodsProcedureDetail.FactoryGoodsProcedureDetailID); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(-1); } }
public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryGoodsProcedureEntities context = CreateContext()) { var dbItem = context.FactoryGoodsProcedure.Where(o => o.FactoryGoodsProcedureID == id).FirstOrDefault(); foreach (var item in dbItem.FactoryGoodsProcedureDetail.ToArray()) { context.FactoryGoodsProcedureDetail.Remove(item); } context.FactoryGoodsProcedure.Remove(dbItem); context.SaveChanges(); return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.FactoryGoodsProcedureDTO dtoFactoryGoodsProcedure = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryGoodsProcedureDTO>(); try { // Check FactoryStep selected or not selected. if (dtoFactoryGoodsProcedure.FactoryGoodsProcedureID.CompareTo(0) != 0) { bool isValid = InvalidFactoryGoodsProcedureDetail(dtoFactoryGoodsProcedure.FactoryGoodsProcedureDetailDTOs); if (!isValid) { throw new Exception("You have select step."); } } using (FactoryGoodsProcedureEntities context = CreateContext()) { FactoryGoodsProcedure dbItem = null; if (id == 0) { dbItem = new FactoryGoodsProcedure(); context.FactoryGoodsProcedure.Add(dbItem); // Generate FactoryGoodsProcedureUD continue. List <string> factoryGoodsProcedureUDs = context.FactoryGoodsProcedure.Select(o => o.FactoryGoodsProcedureUD).ToList(); int nextFactoryGoodsProcedureUD = GenerateFactoryGoodsProcedureUD(factoryGoodsProcedureUDs); string getNext = nextFactoryGoodsProcedureUD.ToString(); if (getNext.Length == 1) { getNext = getNext.PadLeft(2, '0'); } dtoFactoryGoodsProcedure.FactoryGoodsProcedureUD = getNext; } else { dbItem = context.FactoryGoodsProcedure.Where(o => o.FactoryGoodsProcedureID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "Data not found !"; return(false); } string tmpFile = string.Format("{0}{1}", FrameworkSetting.Setting.AbsoluteUserTempFolder, @"\"); converter.DTO2DB_FactoryGoodsProcedure(dtoFactoryGoodsProcedure, ref dbItem); context.FactoryGoodsProcedureDetail.Local.Where(o => o.FactoryGoodsProcedure == null).ToList().ForEach(o => context.FactoryGoodsProcedureDetail.Remove(o)); context.SaveChanges(); dtoItem = GetData(dbItem.FactoryGoodsProcedureID, out notification).Data; return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }