public override bool DeleteData(int id, out DTO.Framework.Notification notification) { notification = new DTO.Framework.Notification() { Type = DTO.Framework.NotificationType.Success }; try { using (MaterialMngEntities context = CreateContext()) { Material dbItem = context.Material.FirstOrDefault(o => o.MaterialID == id); if (dbItem == null) { notification.Message = "Material not found!"; return(false); } else { context.Material.Remove(dbItem); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification.Type = DTO.Framework.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (MaterialMngEntities context = CreateContext()) { Material dbItem = context.Material.FirstOrDefault(o => o.MaterialID == id); if (dbItem == null) { notification.Message = "Material not found!"; return(false); } else { var item = context.MaterialMng_MaterialCheck_View.Where(o => o.MaterialID == id).FirstOrDefault(); //CheckPermission if (item.isUsed.Value == true) { throw new Exception("You can't delete because it used in item other!"); } context.Material.Remove(dbItem); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public override bool UpdateData(int id, ref DTO.MaterialMng.Material dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; int number; string indexName; try { using (MaterialMngEntities context = CreateContext()) { Material dbItem = null; if (id == 0) { dbItem = new Material(); context.Material.Add(dbItem); } else { dbItem = context.Material.FirstOrDefault(o => o.MaterialID == id); var item = context.MaterialMng_MaterialCheck_View.Where(o => o.MaterialID == id).FirstOrDefault(); //CheckPermission if (item.isUsed.Value == true) { throw new Exception("You can't update because it used in item other!"); } } if (dbItem == null) { notification.Message = "Material not found!"; return(false); } else { converter.DTO2BD_Material(dtoItem, ref dbItem); if (id <= 0) { // generate code using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM Material WITH (TABLOCKX, HOLDLOCK)"); try { var newCode = context.MaterialMng_function_GenerateCode().FirstOrDefault(); if (newCode != "**") { dbItem.MaterialUD = newCode; } else { throw new Exception("Auto generated code exceed maximum option: [ZZ]"); } context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } } else { context.SaveChanges(); } dtoItem = GetData(dbItem.MaterialID, out notification).Data; return(true); } } } catch (System.Data.DataException dEx) { notification.Type = Library.DTO.NotificationType.Error; Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName); if (number == 2601 && !string.IsNullOrEmpty(indexName)) { if (indexName == "MaterialUDUnique") { notification.Message = "The Material Code is already exists"; } } else { notification.Message = dEx.Message; } return(false); } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public override bool UpdateData(int id, ref DTO.MaterialMng.Material dtoItem, out DTO.Framework.Notification notification) { notification = new DTO.Framework.Notification() { Type = DTO.Framework.NotificationType.Success }; int number; string indexName; try { using (MaterialMngEntities context = CreateContext()) { Material dbItem = null; if (id == 0) { dbItem = new Material(); context.Material.Add(dbItem); } else { dbItem = context.Material.FirstOrDefault(o => o.MaterialID == id); } if (dbItem == null) { notification.Message = "Material not found!"; return(false); } else { converter.DTO2BD_Material(dtoItem, ref dbItem); context.SaveChanges(); dtoItem = GetData(dbItem.MaterialID, out notification).Data; return(true); } } } catch (System.Data.DataException dEx) { notification.Type = DTO.Framework.NotificationType.Error; Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName); if (number == 2601 && !string.IsNullOrEmpty(indexName)) { if (indexName == "MaterialUDUnique") { notification.Message = "The Material Code is already exists"; } } else { notification.Message = dEx.Message; } return(false); } catch (Exception ex) { notification.Type = DTO.Framework.NotificationType.Error; notification.Message = ex.Message; return(false); } }