Beispiel #1
0
        //-> Create
        public async Task <ItemViewDTO> New(ItemNewDTO newDTO)
        {
            newDTO = StringHelper.TrimStringProperties(newDTO);
            var record = (tblItem)MappingHelper.MapDTOToDBClass <ItemNewDTO, tblItem>(newDTO, new tblItem());

            record.createdDate = DateTime.Now;
            db.tblItems.Add(record);

            await db.SaveChangesAsync();

            db.Entry(record).Reload();
            return(await SelectByID(record.id));
        }
Beispiel #2
0
 public async Task <IHttpActionResult> Create(ItemNewDTO item)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(BadRequest(ModelState));
         }
         return(Ok(await repository.Create(item)));
     }
     catch (HttpException ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Beispiel #3
0
        //-> create
        public async Task <ItemViewDTO> Create(ItemNewDTO newDTO)
        {
            newDTO = StringHelper.TrimStringProperties(newDTO);
            var checkItemGroup = await db.tblItemGroups.FirstOrDefaultAsync(x => x.itmg_Deleted == null && x.id == newDTO.itemGroup.id); // check whether itemgroup name exist or not

            if (checkItemGroup == null)
            {
                throw new HttpException((int)HttpStatusCode.BadRequest, "Item group not exist");
            }

            var checkItemCode = await db.tblItems.FirstOrDefaultAsync(r => r.item_Deleted == null && r.code == newDTO.code); // check whether itemgroup name exist or not

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

            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    if (string.IsNullOrEmpty(newDTO.description))
                    {
                        newDTO.description = newDTO.name;
                    }
                    tblItem item = (tblItem)Helper.Helper.MapDTOToDBClass <ItemNewDTO, tblItem>(newDTO, new tblItem());
                    item.item_CreatedDate = DateTime.Now;
                    item.cost             = 0;
                    item.quantity         = 0;
                    item.lastCost         = 0;

                    db.tblItems.Add(item);
                    await db.SaveChangesAsync();

                    List <sm_doc> documents = await Helper.Helper.SaveUploadImage(db, item.name, Helper.Helper.document_ItemTableID, item.id, newDTO.images);// tmp not useful , just reserve data for using in the furture
                    await SaveItemToWarehouses(item);

                    transaction.Commit();

                    return(await SelectByID(item.id));
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
            }
        }
 public async Task <JsonResult> New(ItemNewDTO newDTO)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.KEY_IN_REQUIRED_FIELD);
         }
         Response.StatusCode = 200;
         return(Json(await handler.New(newDTO), JsonRequestBehavior.AllowGet));
     }
     catch (HttpException)
     {
         return(Json(ConstantHelper.ERROR, JsonRequestBehavior.AllowGet));
     }
 }