Exemplo n.º 1
0
        public JsonResult Post([FromBody] NoteViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var note = Mapper.Map <Note>(vm);
                    note.Created   = DateTime.Now;
                    note.CreatedBy = User.Identity.Name;

                    _repository.AddNote(note);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <NoteViewModel>(note)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new note", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new note"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new employee"));
        }