Esempio n. 1
0
        public ActionResult Patch(string id, [FromBody] NoteUpdateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var ownerId = HttpContext.User.Identity.Name;

            if (!_noteRepository.IsOwner(id, ownerId))
            {
                return(Forbid("You are not the owner of this note"));
            }

            var existingNote = _noteRepository.GetSingle(id);

            existingNote.Title        = model.Title;
            existingNote.LastEditTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
            existingNote.Content      = model.Content;

            _noteRepository.Update(existingNote);
            _noteRepository.Commit();

            return(NoContent());
        }
Esempio n. 2
0
        public ActionResult <NoteCreationViewModel> Post([FromBody] NoteUpdateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var ownerId      = HttpContext.User.Identity.Name;
            var creationTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
            var noteId       = Guid.NewGuid().ToString();
            var note         = new Note
            {
                Id           = noteId,
                Title        = model.Title,
                Content      = model.Content,
                CreationTime = creationTime,
                LastEditTime = creationTime,
                OwnerId      = ownerId,
            };

            _noteRepository.Add(note);
            _noteRepository.Commit();

            return(new NoteCreationViewModel
            {
                NoteId = noteId
            });
        }