public async Task <BreedResult> UpdateBreedAsync(BreedRequest breed, int breedId, int userId) { try { BreedDto breedDto = await _breedRepository.GetByIdAsync(breedId); if (breedDto == null) { return(await BreedResponseNotFound()); } breedDto.Name = breed.Name; breedDto.Status = 1; breedDto.UserModify = userId; breedDto.DateModify = DateTime.Now; await _breedRepository.UpdateAsync(breedDto); return(BreedResponseOK(breedDto)); } catch (Exception) { //TODO Log Error return(await BreedResponseInternalError()); } }
public async Task <BreedResult> AddBreedAsync(BreedRequest newBreed, int userId) { try { BreedDto breedExist = await _breedRepository.GetByNameAsync(newBreed.Name); if (breedExist != null) { return(await BreedResponseDuplicate()); } BreedDto breedDto = _mapper.Map <BreedDto>(newBreed); breedDto.Status = 1; breedDto.UserCreate = userId; breedDto.DateCreate = DateTime.Now; breedDto = await _breedRepository.AddAsync(breedDto); return(BreedResponseOK(breedDto)); } catch (Exception) { //TODO Log Error return(await BreedResponseInternalError()); } }
public async Task <ActionResult <BreedResponse> > Post([FromBody] BreedRequest newBreed) { int userId = int.Parse(HttpContext.GetUserId()); BreedResult result = await _agroBarnSupervisor.AddBreedAsync(newBreed, userId); if (!result.Success) { return(ResponseErrorCode(result)); } BreedResponse breed = _mapper.Map <BreedResponse>(result); var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; var locationUri = baseUrl + "/" + ApiRoutes.Breeds.GetById.Replace("{breedId}", breed.Id.ToString()); return(Created(locationUri, breed)); }
public async Task <ActionResult <BreedResponse> > Update([FromRoute] int breedId, [FromBody] BreedRequest breed) { int userId = int.Parse(HttpContext.GetUserId()); BreedResult result = await _agroBarnSupervisor.UpdateBreedAsync(breed, breedId, userId); if (!result.Success) { return(ResponseErrorCode(result)); } BreedResponse response = _mapper.Map <BreedResponse>(result); return(Ok(response)); }