Пример #1
0
        //Post => api/Notes
        public async Task <IActionResult> Post(NoteViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string userName  = User.Claims.First(o => o.Type == "UserName").Value;
                    var    notesUser = await _userManager.FindByNameAsync(userName);

                    var note = _mapper.Map <Note>(model);
                    if (note.NoteTime == DateTime.MinValue)
                    {
                        note.NoteTime = DateTime.Now;
                    }
                    note.User = notesUser;

                    _notesRepository.AddEntity(note);
                    if (_notesRepository.SaveAll())
                    {
                        return(Created($"/api/notes/", _mapper.Map <NoteViewModel>(note)));
                    }
                    else
                    {
                        return(BadRequest("Failed to save a new note"));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to get note"));
            }
        }