//-> Edit public async Task <ItemViewDTO> Edit(ItemEditDTO editDTO) { editDTO = StringHelper.TrimStringProperties(editDTO); tblItem item = db.tblItems.FirstOrDefault(r => r.item_Deleted == null && r.id == editDTO.id); if (item == null) { throw new HttpException((int)HttpStatusCode.NotFound, "This record has been deleted"); } var checkItemGroup = await db.tblItemGroups.FirstOrDefaultAsync(x => x.itmg_Deleted == null && x.id == editDTO.itemGroup.id); // check whether itemgroup name exist or not if (checkItemGroup == null) { throw new HttpException((int)HttpStatusCode.BadRequest, "Item group not exists"); } var checkItemCode = await db.tblItems.FirstOrDefaultAsync(r => r.item_Deleted == null && r.code == editDTO.code && r.id != editDTO.id); if (checkItemCode != null) { throw new HttpException((int)HttpStatusCode.BadRequest, "This item code already exsits"); } using (var transaction = db.Database.BeginTransaction()) { try { if (string.IsNullOrEmpty(editDTO.description)) { editDTO.description = editDTO.name; } item = (tblItem)Helper.Helper.MapDTOToDBClass <ItemEditDTO, tblItem>(editDTO, item); item.item_UpdatedDate = DateTime.Now; db.Entry(item).State = EntityState.Modified; await db.SaveChangesAsync(); //TODO : need to handle delete images or delete documents List <sm_doc> documents = await Helper.Helper.SaveUploadImage(db, item.name, Helper.Helper.document_ItemTableID, item.id, editDTO.images);// tmp not useful , just reserve data for using in the furture transaction.Commit(); return(await SelectByID(item.id)); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } }
//-> Create public async Task <CustomerViewDTO> Create(CustomerNewDTO newDTO) { newDTO = StringHelper.TrimStringProperties(newDTO); var checkName = await db.tblCustomers.FirstOrDefaultAsync(x => x.cust_Deleted == null && x.name == newDTO.name); // check whether itemgroup name exist or not if (checkName != null) { throw new HttpException((int)HttpStatusCode.BadRequest, "This customer name already exsits."); } tblCustomer customer = (tblCustomer)Helper.Helper.MapDTOToDBClass <CustomerNewDTO, tblCustomer>(newDTO, new tblCustomer()); customer.cust_CreatedDate = DateTime.Now; db.tblCustomers.Add(customer); await db.SaveChangesAsync(); db.Entry(customer).Reload(); return(await SelectByID(customer.id)); }
//-> Edit public async Task <ItemGroupViewDTO> Edit(ItemGroupEditDTO editDTO) { //TODO -- when edit -> //user can add new image or delete current images i mean documents editDTO = StringHelper.TrimStringProperties(editDTO); tblItemGroup itemGroup = db.tblItemGroups.FirstOrDefault(r => r.itmg_Deleted == null && r.id == editDTO.id); if (itemGroup == null) { throw new HttpException((int)HttpStatusCode.NotFound, "This record has been deleted"); } var checkName = await db.tblItemGroups.FirstOrDefaultAsync(r => r.itmg_Deleted == null && r.name == editDTO.name && r.id != editDTO.id); if (checkName != null) { throw new HttpException((int)HttpStatusCode.BadRequest, "This name already exsits"); } using (var transaction = db.Database.BeginTransaction()) { try { itemGroup.itmg_UpdatedDate = DateTime.Now; itemGroup = (tblItemGroup)Helper.Helper.MapDTOToDBClass <ItemGroupEditDTO, tblItemGroup>(editDTO, itemGroup); db.Entry(itemGroup).State = EntityState.Modified; await db.SaveChangesAsync(); List <sm_doc> documents = await Helper.Helper.SaveUploadImage(db, itemGroup.name, Helper.Helper.document_ItemGroupTableID, itemGroup.id, editDTO.images);// tmp not useful , just reserve data for using in the furture transaction.Commit(); return(await SelectByID(itemGroup.id)); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } }