Exemplo n.º 1
0
 public CategoryDTO(CategoryIn categoryIn)
 {
     Name        = categoryIn.Name;
     Description = categoryIn.Description;
     Discount    = categoryIn.Discount;
     Description = categoryIn.Description;
     SGST        = categoryIn.SGST;
     CGST        = categoryIn.CGST;
 }
        public async Task <ActionResult <CategoryOut> > PostCategory([FromForm] CategoryIn categoryIn)
        {
            try
            {
                CategoryDTO categoryDTO = new CategoryDTO(categoryIn);
                _context.Categories.Add(categoryDTO);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetCategory", new { id = categoryDTO.ID }, categoryDTO));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }
        }
        public async Task <IHttpActionResult> PostCategory(CategoryIn categoryIn)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (CategoryTitleExists(categoryIn.Title))
            {
                return(ResponseMessage(getHttpResponse(HttpStatusCode.Conflict)));
            }

            Category newCategory = new Category();

            newCategory.Title       = categoryIn.Title;
            newCategory.Description = categoryIn.Description;
            newCategory.Image       = categoryIn.imageUrl;
            newCategory.IsActive    = 0;
            DateTime now = DateTime.Now;

            newCategory.CreatedAt = now;
            newCategory.UpdatedAt = now;

            db.Categories.Add(newCategory);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (CategoryExists(newCategory.Id))
                {
                    return(ResponseMessage(getHttpResponse(HttpStatusCode.Conflict)));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("WSApi", new { id = newCategory.Id }, newCategory));
        }