Exemple #1
0
        public async Task <ActionResult <SalesReceiptLineModel> > Post(int SalesReceiptId, SalesReceiptLineModel model)
        {
            try
            {
                //Make sure SalesReceiptLineId is not already taken
                var existing = await _repository.GetSalesReceiptLineAsync(SalesReceiptId, model.Id);

                if (existing != null)
                {
                    return(BadRequest("SalesReceiptLine Id in Use"));
                }

                //map
                var SalesReceiptLine = _mapper.Map <SalesReceiptLine>(model);

                //save and return
                if (!await _repository.StoreNewSalesReceiptLineAsync(SalesReceiptId, SalesReceiptLine))
                {
                    return(BadRequest("Bad request, could not create record!"));
                }
                else
                {
                    var location = _linkGenerator.GetPathByAction("Get",
                                                                  "SalesReceiptLine",
                                                                  new { SalesReceiptLine.SalesReceiptId, SalesReceiptLine.Id });

                    return(Created(location, _mapper.Map <SalesReceiptLineModel>(SalesReceiptLine)));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
        }
Exemple #2
0
        public async Task <ActionResult <SalesReceiptLineModel> > Put(int SalesReceiptId, int Id, SalesReceiptLineModel updatedModel)
        {
            try
            {
                var currentSalesReceiptLine = await _repository.GetSalesReceiptLineAsync(SalesReceiptId, Id);

                if (currentSalesReceiptLine == null)
                {
                    return(NotFound($"Could not find SalesReceiptLine with Id of {Id}"));
                }

                _mapper.Map(updatedModel, currentSalesReceiptLine);

                if (await _repository.UpdateSalesReceiptLineAsync(SalesReceiptId, currentSalesReceiptLine))
                {
                    return(_mapper.Map <SalesReceiptLineModel>(currentSalesReceiptLine));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest());
        }