Ejemplo n.º 1
0
 public static Note MapNote(Core.Note note)
 {
     return(new Note
     {
         Id = note.Id,
         AuthorId = note.Author.Id,
         Text = note.Text,
         DateModified = note.DateModified,
         Tags = note.Tags.ToList()
     });
 }
Ejemplo n.º 2
0
        public Task <Core.Note> AddNoteAsync(Core.Note note)
        {
            if (note.Author is null)
            {
                throw new ArgumentException("Note must have author", nameof(note));
            }

            return(AddNoteInternalAsync(note));

            async Task <Core.Note> AddNoteInternalAsync(Core.Note note)
            {
                User author = await _context.Users.FindAsync(note.Author.Id);

                if (author is null)
                {
                    throw new ArgumentException("Author does not exist", nameof(note));
                }

                List <string> tags = await _context.Tags.Select(t => t.Name).ToListAsync();

                var newNote = new Note
                {
                    Id     = note.Id,
                    Author = author,
                    Text   = note.Text
                };

                _context.Notes.Add(newNote);

                ISet <string> tagSet = tags.ToHashSet();

                for (int i = 0; i < note.Tags.Count; i++)
                {
                    if (!tagSet.Contains(note.Tags[i]))
                    {
                        _context.Tags.Add(new Tag {
                            Name = note.Tags[i]
                        });
                    }

                    newNote.NoteTags.Add(new NoteTag {
                        TagName = note.Tags[i], Order = i
                    });
                }

                await _context.SaveChangesAsync();

                return(MapNoteWithAuthorAndTags(newNote));
            }
        }
        public async Task <IActionResult> PostAsync(NewNote newNote)
        {
            if (await _noteRepository.GetUserAsync(newNote.AuthorId) is Core.User author)
            {
                var note = new Core.Note
                {
                    Author = author,
                    Text   = newNote.Text,
                    Tags   = newNote.Tags
                };

                Core.Note result = await _noteRepository.AddNoteAsync(note);

                Note resource = Mapper.MapNote(result);
                return(CreatedAtAction(nameof(GetByIdAsync), new { id = note.Id }, resource));
            }

            ModelState.AddModelError(nameof(newNote.AuthorId), "User does not exist");
            return(BadRequest(ModelState));
        }
Ejemplo n.º 4
0
 public SongNote(Note note, int length)
 {
     Note   = note;
     Length = length;
 }