Exemplo n.º 1
0
        public IActionResult PutModel(int id, [FromBody] Scenario0RequestModel request)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    Logger.LogWarning(nameof(PutModel) + $"(id: {id}, request: {JsonConvert.SerializeObject(request)}) Bad Request"
                                      + Environment.NewLine
                                      + JsonConvert.SerializeObject(ModelState, Formatting.Indented));

                    return(BadRequest(ModelState));
                }

                if (!_data.ContainsKey(id))
                {
                    Logger.LogWarning(nameof(PutModel) + $"(id: {id}, request: {JsonConvert.SerializeObject(request)}) no model with id found");

                    return(NotFound());
                }

                var item = _data[id];
                item.Name  = request.Name;
                item.Date  = request.Date;
                item.Value = request.Value;

                Logger.LogInformation(nameof(PutModel) + $"(id: {id}, request: {JsonConvert.SerializeObject(request)}) success");

                return(Ok(item));
            }
            catch (Exception e)
            {
                Logger.LogError(e, nameof(PutModel) + $"(request: {JsonConvert.SerializeObject(request)})");
                return(StatusCode(500));
            }
        }
Exemplo n.º 2
0
        public IActionResult PostModel([FromBody] Scenario0RequestModel request)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    Logger.LogWarning(nameof(PostModel) + $"(request: {JsonConvert.SerializeObject(request)}) Bad Request"
                                      + Environment.NewLine
                                      + JsonConvert.SerializeObject(ModelState, Formatting.Indented));

                    return(BadRequest(ModelState));
                }

                // for testing exceptions
                if (request.Value < 0)
                {
                    throw new Exception("the test exception because item.Value < 0");
                }

                var item = new Scenario0Model
                {
                    Id    = NextId,
                    Name  = request.Name,
                    Date  = request.Date,
                    Value = request.Value
                };

                _data.Add(item.Id, item);

                Logger.LogInformation(nameof(PostModel) + $"(request: {JsonConvert.SerializeObject(request)}) success");

                return(CreatedAtAction(nameof(GetModel), new { Id = item.Id }, item));
            }
            catch (Exception e)
            {
                Logger.LogError(e, nameof(PostModel) + $"(request: {JsonConvert.SerializeObject(request)})");
                return(StatusCode(500));
            }
        }