示例#1
0
        public async Task <ActionResult <ExpenseLineModel> > Post(int ExpenseId, ExpenseLineModel model)
        {
            try
            {
                //Make sure ExpenseLineId is not already taken
                var existing = await _repository.GetExpenseLineAsync(ExpenseId, model.Id);

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

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

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

                    return(Created(location, _mapper.Map <ExpenseLineModel>(ExpenseLine)));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
        }
示例#2
0
        public async Task <ActionResult <ExpenseLineModel> > Put(int ExpenseId, int Id, ExpenseLineModel updatedModel)
        {
            try
            {
                var currentExpenseLine = await _repository.GetExpenseLineAsync(ExpenseId, Id);

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

                _mapper.Map(updatedModel, currentExpenseLine);

                if (await _repository.UpdateExpenseLineAsync(ExpenseId, currentExpenseLine))
                {
                    return(_mapper.Map <ExpenseLineModel>(currentExpenseLine));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest());
        }