コード例 #1
0
        public virtual async Task <ActionResult <BaseResourceResponse> > UpdateAsync([FromRoute] int id, [FromBody] TEntityDto dto)
        {
            var existing = await _repository.GetByAsync(id);

            if (existing is null)
            {
                Log.Information("Tried to retrieve a entity that don't exist for entity {entityName}. The given id was:{id}", typeof(TEntity).Name, id);
            }
            var updated = _mapper.Map <TEntityDto, TEntity>(dto);

            _mapper.Map(updated, existing);
            _repository.Update(existing);
            try
            {
                var result = await _repository.SaveChangesAsync();

                var resultDto = _mapper.Map <TEntity, TEntityDto>(existing);
                return(Ok(BaseResourceResponse.GetDefaultSuccessResponseWithObject(resultDto)));
            }
            catch (DbUpdateException ex)
            {
                Log.Error("Exception throwed at {className} of entity {entityName} when creating entity: Exception:{@exception}", this.GetType().Name, nameof(TEntity), ex);
                return(StatusCode(500, BaseResourceResponse.GetFailureResponseWithMessage(string.Format("Sorry, a problem occured when trying to update the entity with id {0}", id))));
            }
        }
コード例 #2
0
        public override async Task <ActionResult <BaseResourceResponse> > CreateAsync([FromBody] POSOrderDto transactionDto)
        {
            if (transactionDto is null)
            {
                return(BadRequest("invalid request, body was null"));
            }
            var transaction = _mapper.Map <POSOrderDto, POSOrder>(transactionDto);
            var result      = await _transactionService.CreateTransactionAsync(transaction);

            if (result.Success)
            {
                return(Ok(BaseResourceResponse.GetDefaultSuccessResponseWithObject(_mapper.Map <POSOrder, POSOrderDto>(result.Value))));
            }
            return(BadRequest(BaseResourceResponse.GetDefaultFailureResponseWithObject(_mapper.Map <POSOrder, POSOrderDto>(result.Value), "Couldn't create transaction")));
        }
コード例 #3
0
        public virtual async Task <ActionResult <BaseResourceResponse> > GetAsync([FromRoute] int id)
        {
            var entity = await _repository.Query().Where(e => e.Id == id).FirstOrDefaultAsync();

            if (entity == null)
            {
                Log.Information("GetByAsync call with {idType} id {id} returned a null result", id.GetType().Name, id);
                return(NotFound(BaseResourceResponse.GetFailureResponseWithMessage($"entity with id {id} was not found")));
            }
            try
            {
                Log.Information("Mapping...");
                var entityDto = _mapper.Map <TEntity, TEntityDto>(entity);
                return(Ok(BaseResourceResponse.GetDefaultSuccessResponseWithObject(entityDto)));
            }
            catch (Exception ex)
            {
                Log.Error("Exception throwed at {className} of entity {entityName} when creating entity: Exception:{@ex}", this.GetType().Name, nameof(TEntity), ex);
                return(StatusCode(500, BaseResourceResponse.GetFailureResponseWithMessage(string.Format("Sorry, a problem occured when trying to search for the entity with id {0}", id))));
            }
        }
コード例 #4
0
        public virtual async Task <ActionResult <BaseResourceResponse> > CreateAsync(TEntityDto entityDto)
        {
            var entity = _mapper.Map <TEntityDto, TEntity>(entityDto);

            try
            {
                _repository.Add(entity);
                var result = await _repository.SaveChangesAsync();

                if (result < 0)
                {
                    return(StatusCode(500, BaseResourceResponse.GetDefaultFailureResponseWithObject <TEntityDto>(_mapper.Map <TEntity, TEntityDto>(entity), string.Format("Sorry, a problem occured when trying to create a new entry for the given object "))));
                }
                return(Ok(BaseResourceResponse.GetDefaultSuccessResponseWithObject(_mapper.Map <TEntity, TEntityDto>(entity))));
            }
            catch (Exception ex)
            {
                Log.Error("Exception throwed at {className} for entity {entityName} when creating entity: Exception:{@exception}\n entity object:{@entity}", this.GetType().Name, nameof(TEntity), ex, entity);
                return(StatusCode(500, BaseResourceResponse.GetDefaultFailureResponseWithObject <TEntityDto>(_mapper.Map <TEntity, TEntityDto>(entity), string.Format("Sorry, a problem occured when trying to create a new entry for the given object "))));
            }
        }
コード例 #5
0
        public virtual async Task <ActionResult <BaseResourceResponse> > DeleteAsync(int id)
        {
            var entity = await _repository.GetByAsync(id);

            if (entity is null)
            {
                Log.Information("GetByAsync call with {idType} id {id} returned a null result", id.GetType().Name, id);
                return(NotFound(BaseResourceResponse.GetFailureResponseWithMessage($"entity with id {id} was not found")));
            }
            try
            {
                _repository.Delete(entity);
                _repository.SaveChanges();
                return(Ok(BaseResourceResponse.DefaultSuccessResponse));
            }
            catch (DbUpdateException ex)
            {
                Log.Error("Exception throwed at {className} of entity {entityName} when trying to delete entity. Exception:{@exception} \n entity object:{@entity}", this.GetType().Name, nameof(TEntity), ex, entity);
                return(StatusCode(500, BaseResourceResponse.GetDefaultFailureResponseWithObject <TEntityDto>(_mapper.Map <TEntity, TEntityDto>(entity), string.Format("Sorry, a problem occured when trying to delete the entity with id {0}", id))));
            }
        }
コード例 #6
0
        public async Task <ActionResult <BaseResourceResponse <IList <ProductDto> > > > GetProductsByName([FromQuery] string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                Log.Information("Error 404 when trying to search for Products with given name, name parameter was null.");
                return(StatusCode(404, BaseResourceResponse.GetFailureResponseWithMessage("name parameter is null")));
            }
            var products = await _ProductService.SearchProductsByNameAsync(name);

            if (!products.Any())
            {
                Log.Information("404 error returned on ProductsController for name {name}", name);
                return(StatusCode(404, new BaseResourceResponse <IList <ProductDto> >
                {
                    Message = $"there is no Product with the pattern {name} on it's name",
                    Success = false,
                }));
            }
            var ProductDtos = _mapper.Map <List <Product>, IList <ProductDto> >(products.ToList());
            var resultValue = new BaseResourceResponse <IList <ProductDto> >("Products created with success", ProductDtos);

            return(Ok(resultValue));
        }
コード例 #7
0
 public BaseResourceResponse(BaseResourceResponse response, T resultObject) : base(response)
 {
     ResultObject = resultObject;
 }
コード例 #8
0
 public BaseResourceResponse(BaseResourceResponse response) : this(response?.Message ?? "A unexpected error occurred", response?.Success ?? false)
 {
 }