Пример #1
0
        //-> Create
        public async Task <ItemGroupViewDTO> Create(ItemGroupNewDTO newDTO)
        {
            newDTO = StringHelper.TrimStringProperties(newDTO);
            var checkName = await db.tblItemGroups.FirstOrDefaultAsync(r => r.itmg_Deleted == null && r.name == newDTO.name); // check whether itemgroup name exist or not

            if (checkName != null)
            {
                throw new HttpException((int)HttpStatusCode.BadRequest, "This name already exsits");
            }

            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    tblItemGroup itemGroup = (tblItemGroup)Helper.Helper.MapDTOToDBClass <ItemGroupNewDTO, tblItemGroup>(newDTO, new tblItemGroup());
                    itemGroup.itmg_CreatedDate = DateTime.Now;
                    db.tblItemGroups.Add(itemGroup);
                    await db.SaveChangesAsync();

                    List <sm_doc> documents = await Helper.Helper.SaveUploadImage(db, itemGroup.name, Helper.Helper.document_ItemGroupTableID, itemGroup.id, newDTO.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);
                }
            }
        }
Пример #2
0
 public async Task <IHttpActionResult> Create(ItemGroupNewDTO itemGroup)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(BadRequest(ModelState));
         }
         return(Ok(await repository.Create(itemGroup)));
     }
     catch (HttpException ex)
     {
         return(BadRequest(ex.Message));
     }
 }