/// <summary>
        /// Creates the animal asynchronous.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="userId">The user identifier.</param>
        /// <returns>Status for the creating</returns>
        /// <exception cref="ExceptionDto">There was an error when storing the animal information.</exception>
        public async Task <int> CreateAnimalAsync(ZooAnimalRegisterDto obj, Guid userId)
        {
            try
            {
                var animal = _imapper.Map <ZooAnimalRegisterDto, Animal>(obj);
                animal.DateCreated = DateTime.Now;
                animal.UserCreated = userId;
                _crudAnimalRepository.Add(animal);
                await _crudAnimalRepository.SaveChangesAsync();

                return(animal.Id);
            }
            catch (Exception ex)
            {
                var guid = Guid.NewGuid();
                _logger.Log(LogLevel.Error, ex, guid.ToString());
                throw new ExceptionDto(guid, "There was an error when storing the animal information.");
            }
        }
        /// <summary>
        /// Updates the animal asynchronous.
        /// </summary>
        /// <param name="animalId">The animal identifier.</param>
        /// <param name="obj">The object.</param>
        /// <param name="userId">The user identifier.</param>
        /// <returns>Status for the updating</returns>
        /// <exception cref="ExceptionDto">There was an error when update the animal information.</exception>
        public async Task <int> UpdateAnimalAsync(int animalId, ZooAnimalRegisterDto obj, Guid userId)
        {
            try
            {
                var animal = await _getAnimalRepository.Get().FirstOrDefaultAsync(d => d.Id == animalId);

                _imapper.Map(obj, animal);
                animal.DateUpdated = DateTime.Now;
                animal.UserUpdated = userId;
                _crudAnimalRepository.Update(animal);
                await _crudAnimalRepository.SaveChangesAsync();

                return(animal.Id);
            }
            catch (Exception ex)
            {
                var guid = Guid.NewGuid();
                _logger.Log(LogLevel.Error, ex, guid.ToString());
                throw new ExceptionDto(guid, "There was an error when update the animal information.");
            }
        }
        /// <summary>
        /// Updates the animal asynchronous.
        /// </summary>
        /// <param name="animalId">The animal identifier.</param>
        /// <param name="obj">The object.</param>
        /// <param name="userId">The user identifier.</param>
        /// <returns>Status for the updatig</returns>
        /// <exception cref="ExceptionDto">There was an error when storing the animal information.</exception>
        public async Task <int> UpdateAnimalAsync(int animalId, ZooAnimalRegisterDto obj, Guid userId)
        {
            try
            {
                var animalDto = await _animalDal.GetAnimalsByPredicateAsync(d => d.Code == obj.Code && d.Id != animalId);

                if (animalDto != null)
                {
                    throw new ExceptionDto(Guid.NewGuid(), "The animal code is already in use.");
                }
                return(await _animalDal.UpdateAnimalAsync(animalId, obj, userId));
            }
            catch (ExceptionDto)
            {
                throw;
            }
            catch (Exception ex)
            {
                var guid = Guid.NewGuid();
                _logger.Log(LogLevel.Error, ex, guid.ToString());
                throw new ExceptionDto(guid, "There was an error when update the animal information.");
            }
        }
示例#4
0
        public async Task <IActionResult> CreatePositionAsync([FromBody] ZooAnimalRegisterDto obj)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new ResponseErrorDto((int)HttpStatusCode.BadRequest, "Review Required Parameters")));
                }
                var created = await _animalService.CreateAnimalAsync(obj, User.GetIdUser());

                return(CreatedAtAction(nameof(GetAnimalByIdAsync).Replace("Async", string.Empty), new { id = created }, new ResponseDto <ZooAnimalRegisterDto>((int)HttpStatusCode.Created, "Ok", obj)));
            }
            catch (ExceptionDto exdto)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ResponseErrorDto((int)HttpStatusCode.InternalServerError, exdto.UserMessage, exdto.Id)));
            }
            catch (Exception ex)
            {
                var guid = Guid.NewGuid();
                _logger.Log(LogLevel.Error, ex, guid.ToString());
                return(BadRequest(new ResponseErrorDto((int)HttpStatusCode.BadRequest, "Failed to create the animal information.", guid)));
            }
        }
示例#5
0
        public async Task <IActionResult> UpdatePositionAsync([FromRoute][Required] int id, [FromBody] ZooAnimalRegisterDto obj)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new ResponseErrorDto((int)HttpStatusCode.BadRequest, "Review Required Parameters")));
                }
                var updated = await _animalService.UpdateAnimalAsync(id, obj, User.GetIdUser());

                return(Ok(new ResponseDto <ZooAnimalRegisterDto>((int)HttpStatusCode.OK, "Ok", obj)));
            }
            catch (ExceptionDto exdto)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ResponseErrorDto((int)HttpStatusCode.InternalServerError, exdto.UserMessage, exdto.Id)));
            }
            catch (Exception ex)
            {
                var guid = Guid.NewGuid();
                _logger.Log(LogLevel.Error, ex, guid.ToString());
                return(BadRequest(new ResponseErrorDto((int)HttpStatusCode.BadRequest, "Failed to create the animal information.", guid)));
            }
        }