public void DTO2BD(DTO.SeatCushion dtoItem, ref SeatCushion dbItem) { AutoMapper.Mapper.Map <DTO.SeatCushion, SeatCushion>(dtoItem, dbItem); // Tri Add created date for the Seat Cushion if (dtoItem.SeatCushionID == 0) { dbItem.CreatedBy = dtoItem.CreatedBy; dbItem.CreatedDate = DateTime.Now; } else { dbItem.UpdatedBy = dtoItem.UpdatedBy; dbItem.UpdatedDate = DateTime.Now; } // map child if (dtoItem.ProductGroups != null) { // map child rows foreach (DTO.ProductGroup dtoGroup in dtoItem.ProductGroups) { SeatCushionProductGroup dbGroup; if (dtoGroup.SeatCushionProductGroupID <= 0) { dbGroup = new SeatCushionProductGroup(); dbItem.SeatCushionProductGroup.Add(dbGroup); } else { dbGroup = dbItem.SeatCushionProductGroup.FirstOrDefault(o => o.SeatCushionProductGroupID == dtoGroup.SeatCushionProductGroupID); } if (dbGroup != null) { AutoMapper.Mapper.Map <DTO.ProductGroup, SeatCushionProductGroup>(dtoGroup, dbGroup); } } } }
public bool Reset(int id, ref DTO.SeatCushion dtoItem, out Library.DTO.Notification notification) { throw new NotImplementedException(); }
public bool UpdateData(int id, ref object dtoItem, int iRequesterID, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.SeatCushion dtoSeatCushion = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.SeatCushion>(); try { using (SeatCushionMngEntities context = CreateContext()) { SeatCushion dbItem = null; if (id == 0) { dtoSeatCushion.CreatedBy = iRequesterID; dbItem = new SeatCushion(); context.SeatCushion.Add(dbItem); } else { var item = context.SeatCushionMng_SeatCushionCheck_View.Where(o => o.SeatCushionID == id).FirstOrDefault(); //CheckPermission if (item.isUsed.Value == true) { throw new Exception("You can't update because it used in item other!"); } dtoSeatCushion.UpdatedBy = iRequesterID; dbItem = context.SeatCushion.FirstOrDefault(o => o.SeatCushionID == id); } if (dbItem == null) { notification.Message = "Back cushion not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoSeatCushion.ConcurrencyFlag_String))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } converter.DTO2BD(dtoSeatCushion, ref dbItem); // processing image if (dtoSeatCushion.ImageFile_HasChange) { dbItem.ImageFile = fwFactory.CreateFilePointer(this._TempFolder, dtoSeatCushion.ImageFile_NewFile, dtoSeatCushion.ImageFile); } if (id <= 0) { // generate code using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM SeatCushion WITH (TABLOCKX, HOLDLOCK)"); try { var newCode = context.SeatCushionMng_function_GenerateCode().FirstOrDefault(); if (newCode != "*") { dbItem.SeatCushionUD = newCode; } else { throw new Exception("Auto generated code exceed maximum option: [Z]"); } context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } } else { context.SaveChanges(); } // Handle notification missing information. string emailSubject = (id == 0) ? "TASK REQUEST [CREATE SEAT CUSHION]" : "TASK REQUEST [UPDATE SEAT CUSHION]"; string emailBody = string.Empty; if (!IsNullPropertiesSeatCushion(dbItem, ref emailBody)) { SendToEmailNotification(context, emailSubject, emailBody); } dtoItem = GetData(dbItem.SeatCushionID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }