public async Task <BaseDtoResponse <FoodCategoryDto> > Update(Guid id, FoodCategoryCreateRequest request) { try { FoodCategory category = await _foodCategoryRepository.GetById(id); if (category != null) { category.Description = request.Description; category.Name = request.Name; await _foodCategoryRepository.Update(category); FoodCategory updatedResult = await _foodCategoryRepository.GetById(id); FoodCategoryDto result = _mapper.Map <FoodCategory, FoodCategoryDto>(updatedResult); return(new BaseDtoResponse <FoodCategoryDto>(result)); } else { return(new BaseDtoResponse <FoodCategoryDto>("Food Category Not found")); } } catch (Exception ex) { return(new BaseDtoResponse <FoodCategoryDto>($"An error occurred when updating the category: {ex.Message}")); } }
public async Task <IActionResult> Update(Guid id, [FromBody] FoodCategoryCreateRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } try { var response = await _foodCategorySerive.Update(id, request); return(Ok(response)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <BaseDtoResponse <FoodCategoryDto> > Add(FoodCategoryCreateRequest request) { try { FoodCategory model = _mapper.Map <FoodCategoryCreateRequest, FoodCategory>(request); FoodCategory category = await _foodCategoryRepository.Add(model); if (category != null) { FoodCategoryDto result = _mapper.Map <FoodCategory, FoodCategoryDto>(category); return(new BaseDtoResponse <FoodCategoryDto>(result)); } else { return(new BaseDtoResponse <FoodCategoryDto>("Unable to create a new category, try again")); } } catch (Exception ex) { return(new BaseDtoResponse <FoodCategoryDto>($"An error occurred when saving the category: {ex.Message}")); } }