public IResult Insert() { var result = new Result { Operation = Operation.Create, Status = Status.Success }; try { if (!ModelState.IsValid) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } var product = JsonConvert.DeserializeObject <Products>(Request.Form["product"]); IFormFile img = null; if (Request.Form.Files.Count != 0) { var image = Request.Form.Files; img = image[0]; } var pdtNameCheck = _context.Products.Where(p => p.ProductName == product.ProductName && p.ProductId != product.ProductId && p.IsDeleted != true).FirstOrDefault(); if (pdtNameCheck != null) { result.Message = "Product of this name exists already"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } var pdtModelCheck = _context.Products.Where(p => p.ModelNumber == product.ModelNumber && p.ProductId != product.ProductId && p.IsDeleted != true).FirstOrDefault(); if (pdtModelCheck != null) { result.Message = "Product with this model number exists"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } product.CreatedDate = DateTime.Now; product.IsActive = true; if (img != null) { ImageExtension imageExtension = new ImageExtension(); product.ProductImage = imageExtension.Image(img); } _context.Products.Add(product); _context.SaveChanges(); result.StatusCode = HttpStatusCode.OK; return(result); } catch (Exception e) { result.Message = e.Message; result.Status = Status.Error; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public IResult Insert() { var result = new Result { Operation = Operation.Create, Status = Status.Success }; try { if (!ModelState.IsValid) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } var category = JsonConvert.DeserializeObject <Categories>(Request.Form["category"]); IFormFile img = null; if (Request.Form.Files.Count != 0) { var image = Request.Form.Files; img = image[0]; } var categoryCheck = _context.Categories.Where(c => c.CategoryName == category.CategoryName && c.CategoryId != category.CategoryId && c.IsDeleted != true).FirstOrDefault(); if (categoryCheck != null) { result.Message = "Category of this name exists"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } category.CreatedDate = DateTime.Now; if (img != null) { ImageExtension imageExtension = new ImageExtension(); category.CategoryImage = imageExtension.Image(img); } _context.Categories.Add(category); _context.SaveChanges(); result.StatusCode = HttpStatusCode.OK; return(result); } catch (Exception e) { result.Message = e.Message; result.Status = Status.Error; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }