public override bool DeleteData(int id, out Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FrameMaterialMngEntities context = CreateContext()) { FrameMaterial dbItem = context.FrameMaterial.FirstOrDefault(o => o.FrameMaterialID == id); if (dbItem == null) { notification.Message = "Frame material not found!"; return(false); } else { var item = context.FrameMaterialMng_FrameMaterialCheck_View.Where(o => o.FrameMaterialID == id).FirstOrDefault(); //CheckPermission if (item.isUsed.Value == true) { throw new Exception("You can't delete because it used in item other!"); } context.FrameMaterial.Remove(dbItem); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public void DTO2BD(DTO.FrameMaterialDto dtoItem, ref FrameMaterial dbItem) { AutoMapper.Mapper.Map <DTO.FrameMaterialDto, FrameMaterial>(dtoItem, dbItem); }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Notification(); notification.Type = NotificationType.Success; FrameMaterialDto dtoFrameMaterial = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <FrameMaterialDto>(); int number; string indexName; try { using (var context = CreateContext()) { FrameMaterial frameMaterial = null; if (id == 0) { frameMaterial = new FrameMaterial(); context.FrameMaterial.Add(frameMaterial); frameMaterial.UpdatedBy = userId; frameMaterial.UpdatedDate = DateTime.Now; } else { var item = context.FrameMaterialMng_FrameMaterialCheck_View.Where(o => o.FrameMaterialID == id).FirstOrDefault(); //CheckPermission if (item.isUsed.Value == true) { throw new Exception("You can't update because it used in item other!"); } frameMaterial = context.FrameMaterial.FirstOrDefault(o => o.FrameMaterialID == id); frameMaterial.UpdatedBy = userId; frameMaterial.UpdatedDate = DateTime.Now; } if (frameMaterial == null) { notification.Message = "Frame Material not found!"; return(false); } else { converter.DTO2BD(dtoFrameMaterial, ref frameMaterial); if (id <= 0) { // Generate code. using (var trans = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM FrameMaterial WITH (TABLOCKX, HOLDLOCK)"); try { var newCode = context.FrameMaterialMng_function_GenerateCode().FirstOrDefault(); if (!"**".Equals(newCode)) { frameMaterial.FrameMaterialUD = newCode; context.SaveChanges(); } else { notification.Type = NotificationType.Error; notification.Message = "Auto generated code exceed maximum option: [ZZ]"; } } catch (Exception ex) { trans.Rollback(); throw ex; } finally { trans.Commit(); } } } else { context.SaveChanges(); } dtoItem = GetData(frameMaterial.FrameMaterialID, out notification).Data; return(true); } } } catch (DataException exData) { notification.Type = NotificationType.Error; ErrorHelper.DataExceptionParser(exData, out number, out indexName); if (number == 2601 && !string.IsNullOrEmpty(indexName)) { if ("FrameMaterialUDUnique".Equals(indexName)) { notification.Message = "The Frame Material Code is already exists."; } } else { notification.Message = exData.Message; } return(false); } catch (Exception ex) { notification.Type = NotificationType.Error; notification.Message = ex.Message; return(false); } }