Exemple #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))));
            }
        }
        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")));
        }
Exemple #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))));
            }
        }
Exemple #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 "))));
            }
        }