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); } }
public IResult Update() { var result = new Result { Operation = Operation.Update, 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 productDetail = _context.Products.Where(p => p.ProductId == product.ProductId && p.IsDeleted != true).FirstOrDefault(); if (productDetail == null) { result.Message = "Product does not exist"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } 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"; 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); } productDetail.ProductName = product.ProductName; productDetail.ProductDescription = product.ProductDescription; productDetail.IsActive = product.IsActive; productDetail.CategoryId = product.CategoryId; productDetail.IsDiscounted = product.IsDiscounted; productDetail.DiscountPercent = product.DiscountPercent; productDetail.ModelNumber = product.ModelNumber; productDetail.Price = product.Price; productDetail.QuantityInStock = product.QuantityInStock; productDetail.CreatedBy = product.CreatedBy; productDetail.CreatedDate = product.CreatedDate; productDetail.ModifiedBy = product.ModifiedBy; productDetail.ModifiedDate = DateTime.Now; if (productDetail.ProductImage != null) { if (img == null) { productDetail.ProductImage = null; } else { var imageExtension = new ImageExtension(); productDetail.ProductImage = imageExtension.Image(img); } } _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 Update() { var result = new Result { Operation = Operation.Update, 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 categoryDetail = _context.Categories.Where(c => c.CategoryId == category.CategoryId && c.IsDeleted != true).FirstOrDefault(); if (categoryDetail == null) { result.Message = "Category does not exist"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } var duplicateName = _context.Categories.Where(c => c.CategoryName == category.CategoryName && c.CategoryId != category.CategoryId && c.IsDeleted != true).FirstOrDefault(); if (duplicateName != null) { result.Message = "Category of this name exists"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } categoryDetail.CategoryName = category.CategoryName; categoryDetail.CategoryDescription = category.CategoryDescription; categoryDetail.IsActive = category.IsActive; categoryDetail.CreatedBy = category.CreatedBy; categoryDetail.CreatedDate = category.CreatedDate; categoryDetail.ModifiedBy = category.ModifiedBy; categoryDetail.ModifiedDate = DateTime.Now; if (categoryDetail.CategoryImage != null) { if (img == null) { categoryDetail.CategoryImage = null; } else { var imageExtension = new ImageExtension(); categoryDetail.CategoryImage = imageExtension.Image(img); } } _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 CreateUser() { var result = new Result() { Operation = Operation.Create, Status = Status.Success }; try { if (!ModelState.IsValid) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } IFormFile img = null; var userModel = JsonConvert.DeserializeObject <Users>(Request.Form["model"]); var image = Request.Form.Files; foreach (var i in image) { img = image[0]; } if (userModel != null) { if (img == null) { result.Message = "User image does not exist"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } var userDetailCheck = _context.Users.Where(u => (u.Username == userModel.Username) || (u.EmailId == userModel.EmailId)).FirstOrDefault(); if (userDetailCheck != null) { if (userDetailCheck.Username == userModel.Username) { result.Message = "Username is already taken"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } else { result.Message = "Email ID is already registered"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } } ImageExtension imageExtension = new ImageExtension(); userModel.UserImage = imageExtension.Image(img); _context.Users.Add(userModel); _context.SaveChangesAsync(); return(result); } result.Message = "Invalid user details entered"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }