Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] GoalDTO goal)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // would like to handle notfound in contexgt

            // Convert from DTO to internal model, now Entity
            // Will probably use Automapper

            Goals.API.Core.Entities.Goal goalEntity = new Core.Entities.Goal()
            {
                Id          = goal.Id,
                Name        = goal.Name,
                Description = goal.Description,
                TargetDate  = goal.TargetDate
            };

            try
            {
                await _repository.AddGoalAsync(goalEntity);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, $"Error posting goal, see Stack trace:  {ex.StackTrace}");
                throw ex;
            }
            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Put(int id, [FromBody] GoalDTO goal)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != goal.Id)
            {
                return(BadRequest());
            }

            // would like to handle notfound in contexgt

            Goals.API.Core.Entities.Goal goalEntity = new Core.Entities.Goal()
            {
                Id          = goal.Id,
                Name        = goal.Name,
                Description = goal.Description,
                TargetDate  = goal.TargetDate
            };

            try
            {
                await _repository.UpdateGoalAsync(goalEntity);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, $"Error putting Goal, see Stack Trace {ex.StackTrace}");

                throw ex;
            }
            return(NoContent());
        }