public async Task <ApiResult <string> > CreateCategoryAsync(CategoryForCreationDto categoryDto) { try { if (categoryDto != null) { var newId = Guid.NewGuid().ToString("D"); var newCategory = ObjectMapper.Mapper.Map <CategoryForCreationDto, eQACoLTD.Data.Entities.Category>(categoryDto); newCategory.Id = newId; await _context.Categories.AddAsync(newCategory); await _context.SaveChangesAsync(); return(new ApiResult <string>(HttpStatusCode.OK) { ResultObj = newId, Message = "Tạo danh mục thành công" }); } return(new ApiResult <string>(HttpStatusCode.BadRequest)); } catch { return(new ApiResult <string>(HttpStatusCode.InternalServerError, "Có lỗi khi thêm danh mục")); } }
public IActionResult CreateCategory([FromBody] CategoryForCreationDto category) { try { if (category == null) { _logger.LogError("category object sent from client is null."); return(BadRequest("category object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid category object sent from client."); return(BadRequest("Invalid model object")); } var categoryEntity = _mapper.Map <Category>(category); _repository.Category.CreateCategory(categoryEntity); _repository.Save(); var createdCategory = _mapper.Map <CategoryDto>(categoryEntity); return(CreatedAtRoute("CategoryById", new { id = createdCategory.id }, createdCategory)); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CreateCategory action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public IActionResult CreateCategory(CategoryForCreationDto category) { if (!ModelState.IsValid || category == null) { return(BadRequest(ModelState)); } var categoryToSave = new Category(); AutoMapper.Mapper.Map(category, categoryToSave); var imgUrl = _s3Service.UploadFile(category.Image); if (!string.IsNullOrEmpty(imgUrl)) { categoryToSave.ImgUrl = imgUrl; } _viLabRepository.AddCategory(categoryToSave); if (!_viLabRepository.Save()) { return(BadRequest($"failed to create category {category.Name}")); } return(Ok()); }
public async Task <IActionResult> CreateCategory(int projectId, CategoryForCreationDto categoryForCreationDto) { if (!await _projectRepo.ExistProject(projectId)) { return(Unauthorized()); } if (await _repo.ExistCategory(categoryForCreationDto.Name)) { return(BadRequest("Category already exists")); } var projectFromRepo = await _projectRepo.GetProject(projectId); var categoryToCreate = _mapper.Map <Category>(categoryForCreationDto); var categoryDirectoryInfo = await _fileRepository.CreateCategoryDirectory(projectFromRepo.Name, categoryToCreate.Name); categoryToCreate.Path = categoryDirectoryInfo.ToString(); if (await _repo.CreateCategory(projectId, categoryToCreate)) { var categoryToReturn = _mapper.Map <CategoryToReturnDto>(categoryToCreate); return(CreatedAtRoute("GetCategory", new { projectId, id = categoryToCreate.Id }, categoryToReturn)); } throw new System.Exception("Creating the category failed"); }
public IResult InsertCategory(CategoryForCreationDto categoryForCreationDto) { var categoryToBeInserted = _mapper.Map <CategoryTranslation>(categoryForCreationDto); _categoryTranslationRepository.Insert(categoryToBeInserted); return(new SuccessResult(string.Format(Messages.SuccessfulInsert, nameof(Category)))); }
public async Task <IActionResult> CreateCategory(CategoryForCreationDto categoryForCreationDto) { var validationErrors = categoryForCreationDto.Validate(); if (validationErrors.Any()) { return(ValidationProblem(validationErrors.First())); } var categoriesFromQueryHandler = await Mediator.Send(new GetAllCategoriesQuery()); var categoryFromMemory = categoriesFromQueryHandler.FirstOrDefault(x => x.Name == categoryForCreationDto.Name) ?? EmptyCategory; if (categoryFromMemory != EmptyCategory) { return(ValidationProblem(CONFLICT_WITH_EXISTING_CATEGORY)); } var categoryToReturn = await Mediator.Send(new CreateCategoryCommand(categoryForCreationDto)); return(CreatedAtRoute(new { categoryId = categoryToReturn.Id }, categoryToReturn)); }
public async Task <ActionResult> AddCategoryForUser([FromBody] CategoryForCreationDto category) { var categoryToAdd = _mapper.Map <Category>(category); var userIdFromToken = User.GetUserIdAsGuid(); await _categoryService.AddCategory(userIdFromToken.ToString(), categoryToAdd); return(Ok()); }
public async Task <IActionResult> CreateCategory([FromBody] CategoryForCreationDto category) { var categoryEntity = _mapper.Map <Category>(category); _repository.Category.CreateCategory(categoryEntity, trackChanges: false); await _repository.SaveAsync(); var categoryToReturn = _mapper.Map <CategoryDto>(categoryEntity); return(CreatedAtRoute("CategoryById", new { id = categoryToReturn.Id }, categoryToReturn)); }
public IActionResult AddCategory([FromBody] CategoryForCreationDto category) { var cat = _mapper.Map <Category>(category); _repository.Add(cat); if (!_repository.Save()) { return(StatusCode(500, "Somethingn wrong with posting category happened!")); } return(CreatedAtRoute("post", new { id = cat.Id }, cat)); }
public ActionResult <TransactionDto> CreateOutcomeCategory(CategoryForCreationDto category) { var categoryEntity = _mapper.Map <Entities.OutcomeCategory>(category); _transactionRepository.AddOutcomeCategory(categoryEntity); _transactionRepository.Save(); var categoryToReturn = _mapper.Map <CategoryDto>(categoryEntity); return(CreatedAtRoute("GetCategory", new { categoryId = categoryToReturn.Id }, categoryToReturn)); }
public async Task <IActionResult> AddCategory(CategoryForCreationDto categoryForCreationDto) { var categoryToAdd = _mapper.Map <Category>(categoryForCreationDto); await _context.Categories.AddAsync(categoryToAdd); if (await _context.SaveChangesAsync() > 0) { return(Ok()); } return(BadRequest()); }
public ActionResult <CategoryDto> AddCategory(CategoryForCreationDto categoryForCreation) { var category = _mapper.Map <Category>(categoryForCreation); _categoryRepository.AddCategory(category); _categoryRepository.Save(); var categoryDto = _mapper.Map <CategoryDto>(category); return(CreatedAtRoute("GetCategory", new { categoryDto.CategoryId }, categoryDto)); }
public async Task <IActionResult> EditCategory(int id, CategoryForCreationDto categoryForCreation) { var category = await _repo.GetItemAsync(id); if (category != null) { _mapper.Map(categoryForCreation, category); if (await _repo.SaveChangesAsync() > 0) { return(Ok()); } } return(BadRequest()); }
public async Task <IActionResult> CreateCategory(int userId, CategoryForCreationDto categoryForCreationDto) { categoryForCreationDto.UserId = userId; var command = new CreateCategoryCommand(categoryForCreationDto); var result = await Mediator.Send(command); if (result != null) { return(CreatedAtAction(nameof(GetCategoryById), new { categoryId = result.Id, categoryForCreationDto.UserId }, result)); } throw new Exception("Creating category failed on save."); }
public async Task <CategoryDto> Create(Guid userId, CategoryForCreationDto categoryForCreationDto) { var category = new Data.Category() { Name = categoryForCreationDto.Name, Description = categoryForCreationDto.Description, CreatedBy = userId }; category = await _categoryRepositoryRepository.Add(category); await _categoryRepositoryRepository.CommitAsync(); return(_mapper.Map <Data.Category, CategoryDto>(category)); }
public async Task <IActionResult> CreateCategory([FromBody] CategoryForCreationDto categoryForCreationDto) { if (await _repo.CategoryByNameExist(categoryForCreationDto.Name.ToLower())) { return(BadRequest("Category with that name already exists")); } var category = _mapper.Map <Category>(categoryForCreationDto); _repo.Add(category); if (await _repo.SaveAll()) { var categoryToReturn = _mapper.Map <CategoryToReturnDto>(category); return(CreatedAtRoute("GetCategory", new { categoryId = category.Id }, categoryToReturn)); } return(BadRequest("Creating the category failed.")); }
public IActionResult CreateCategory([FromBody] CategoryForCreationDto categoryForCreationDto) { if (categoryForCreationDto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var categoryEntity = Mapper.Map <Category>(categoryForCreationDto); repository.AddCategory(categoryEntity); repository.Save(); return(Ok()); }
public async Task <IActionResult> AddCategory(CategoryForCreationDto categoryForCreationDto) { if (categoryForCreationDto != null) { var allCategories = _repo.AllItems.ToList(); foreach (var item in allCategories) { if (item.Name == categoryForCreationDto.Name) { return(BadRequest("Наименование категории уже существует")); } } var categoryToCreation = _mapper.Map <Category>(categoryForCreationDto); if (await _repo.AddItemAsync(categoryToCreation)) { var catRet = _mapper.Map <CategoryToReturnDto>(categoryToCreation); return(Ok(catRet)); } } return(BadRequest()); }
public async Task <IActionResult> UpdateCategory(int categoryId, [FromBody] CategoryForCreationDto categoryForCreationDto) { var categoryFromRepo = await _repo.GetCategory(categoryId); if (categoryFromRepo == null) { return(NoContent()); } if (await _repo.CategoryByNameExist(categoryForCreationDto.Name.ToLower())) { return(BadRequest("Category with that name already exists")); } _mapper.Map(categoryForCreationDto, categoryFromRepo); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Updating the category failed.")); }
public async Task <IActionResult> CreateCategory(CategoryForCreationDto categoryDto) { var result = await _categoryService.CreateCategoryAsync(categoryDto); return(StatusCode((int)result.Code, result)); }
public Category AddCategory(CategoryForCreationDto category) { var newCategory = Mapper.Map <Category>(category); return(_categoryRepository.Create(newCategory)); }
public IActionResult CreateCategory(CategoryForCreationDto dto) { var category = _dataService.CreateCategory(dto.Name, dto.Description); return(CreatedAtRoute(null, CreateCategoryDto(category))); }
public CreateCategoryCommand(CategoryForCreationDto categoryForCreationDto) { CategoryForCreationDto = categoryForCreationDto; }