public IActionResult Post([FromBody] ProductCategoryDto productCategoryDto) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var productCategory = Mapper.Map <ProductCategory>(productCategoryDto); if (_productCategoryRepository.ProductCategoryExists(productCategory)) { return(StatusCode(500, "ProductCategory already exists.")); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var profile = _accountRepository.GetUserProfile(userId); var createdProductCategory = _productCategoryRepository.CreateProductCategory(productCategory, profile.UserProfileId); if (createdProductCategory == null) { return(StatusCode(500, "A problem happened while handling your request.")); } var createdProductCategoryToReturn = Mapper.Map <ProductCategoryDto>(createdProductCategory); return(Created(createdProductCategoryToReturn)); } catch (Exception ex) { _logger.LogError($"Failed in Post /ProductCategories: {ex}"); return(StatusCode(500, "A problem happened while handling your request.")); } }
public HttpResponseMessage CreateProductCategory(ProductCategoryModel productCategoryModel) { ResponseModel _response = new ResponseModel(); var _getCategory = _productCategoryRepository.FindBy(x => x.ProductCategoryName.Trim().ToLower() == productCategoryModel.ProductCategoryName.Trim().ToLower() && x.Status == (int)Status.Active && x.IsDeleted == false).FirstOrDefault(); if (_getCategory != null) { _response.Type = "error"; _response.Message = "Company already Exist"; return(Request.CreateResponse(HttpStatusCode.OK, _response)); } var model = Mapper.Map <ProductCategoryModel, ProductCategory>(productCategoryModel); model.CreatedDate = DateTime.Now; model.Status = 1; model.IsDeleted = false; var Result = _productCategoryRepository.CreateProductCategory(model); if (Result != null) { // For Uploading image ---- if (productCategoryModel.DocProductCategory != null) { UploadDocumentModel UploadDocument = new UploadDocumentModel(); UploadDocument.documents = new List <DocumentModel>(); UploadDocument.ImageSize = 250; UploadDocument.ImageQuality = 250; UploadDocument.documents.Add(productCategoryModel.DocProductCategory); List <string> uploadedFileName = SavePicture(UploadDocument); if (uploadedFileName.Count > 0) { ProductCategory _productCategory = _productCategoryRepository.FindBy(x => x.ProductCategoryId == Result.ProductCategoryId).FirstOrDefault(); if (_productCategory != null) { _productCategory.ProductCategoryImagePath = uploadedFileName[0].ToString(); _productCategoryRepository.Update(_productCategory); _productCategoryRepository.SaveChanges(); } } } _response.Type = "success"; _response.Message = "Company created successfully"; return(Request.CreateResponse(HttpStatusCode.OK, _response)); } else { _response.Type = "error"; _response.Message = "Something went wrong"; return(Request.CreateResponse(HttpStatusCode.OK, _response)); } }