コード例 #1
0
        public async Task <ActionResult> Store([FromBody] NoteViewModel model)
        {
            ValidateRequest(model);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var note = model.MapEntity(_mapper, CurrentUserId);

            note = await _notesService.CreateAsync(note);

            if (model.Attachments.HasItems())
            {
                var attachments = model.Attachments.Select(item => item.MapEntity(_mapper, CurrentUserId)).ToList();
                foreach (var attachment in attachments)
                {
                    attachment.PostType = PostType.Note;
                    attachment.PostId   = note.Id;
                }

                _attachmentsService.CreateMany(attachments);

                note.Attachments = attachments;
            }


            return(Ok(note.MapViewModel(_mapper)));
        }
コード例 #2
0
        public async Task <ActionResult> Update(int id, [FromBody] NoteViewModel model)
        {
            var existingEntity = await _notesService.GetByIdAsync(id);

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

            ValidateRequest(model);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var note = model.MapEntity(_mapper, CurrentUserId);

            await _notesService.UpdateAsync(existingEntity, note);


            if (model.Attachments.HasItems())
            {
                var attachments = model.Attachments.Select(item => item.MapEntity(_mapper, CurrentUserId)).ToList();
                foreach (var attachment in attachments)
                {
                    attachment.PostType = PostType.Note;
                    attachment.PostId   = note.Id;
                }

                await _attachmentsService.SyncAttachmentsAsync(note, attachments);

                note.Attachments = attachments;
            }
            else
            {
                await _attachmentsService.SyncAttachmentsAsync(note, null);
            }

            return(Ok());
        }