Пример #1
0
        public async Task <IActionResult> UpdateNote(int id, [FromBody] UpdateNoteModel updateNote)
        {
            if (updateNote == null || updateNote.PostID != id)
            {
                return(BadRequest("Id's do not match"));
            }

            //var user = new User { ID = 1, };
            var note = await _NoteRepository.Get(id);

            if (note == null)
            {
                return(NotFound());
            }

            note.Text   = updateNote.Text;
            note.UserID = updateNote.UserID;
            note.PostID = updateNote.PostID;
            var result = await _NoteRepository.Update(note);

            if (result)
            {
                return(Ok());
            }
            else
            {
                BadRequest("something went wrong");
            }

            return(Ok());
        }
        public async Task <ActionResult> UpdateAsync([FromBody] UpdateNoteModel model)
        {
            var ownerId = HttpContext.GetSessionUserId();

            var note = await _noteService.UpdateNoteAsync(model, ownerId);

            return(Ok(note));
        }
Пример #3
0
 public IActionResult Update([FromBody] UpdateNoteModel model)
 {
     try
     {
         var note = _notes.Get(model.Id);
         if (note == null)
         {
             return(NotFound());
         }
         _mapper.Map(model, note);
         _notes.Edit(note);
         var data = _mapper.Map <NoteModel>(note);
         return(Ok(data));
     }
     catch (NotFoundException)
     {
         return(NotFound());
     }
 }
Пример #4
0
        public async Task <NoteModel> UpdateNoteAsync(UpdateNoteModel model, Guid ownerId)
        {
            var noteEntity = await _noteRepository.FindOneAsync(t => t.Id == model.Id);

            if (noteEntity == null)
            {
                throw model.Id.EntityNotFoundException();
            }

            if (noteEntity.CreatedBy != ownerId)
            {
                throw model.Id.EntityNotFoundException();
            }

            noteEntity.Tags  = (await _tagRepository.InsertTagsAsync(model.Tags, ownerId)).ToList();
            noteEntity.Title = model.Title;
            noteEntity.Text  = model.Text;

            var updatedNote = await _noteRepository.InsertOrUpdateAsync(noteEntity, noteEntity.CreatedBy);

            return(_mapper.Map <Note, NoteModel>(updatedNote));
        }
Пример #5
0
        public async Task <NoteModel> CreateOrUpdate(UpdateNoteModel updateModel)
        {
            var model = await _dbContext.Notes.SingleOrDefaultAsync(x => x.Id == updateModel.Id);

            if (model == null)
            {
                model = new NoteModel
                {
                    Id        = updateModel.Id,
                    Text      = updateModel.Text,
                    CreatedAt = DateTime.UtcNow,
                    UpdatedAt = DateTime.UtcNow
                };
                await _dbContext.Notes.AddAsync(model);
            }
            else
            {
                model.Text      = updateModel.Text;
                model.UpdatedAt = DateTime.UtcNow;
            }
            await _dbContext.SaveChangesAsync();

            return(model);
        }