Exemplo n.º 1
0
        public async Task <IActionResult> PutTag([FromRoute] int id, [FromBody] Tag tag)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tag.Id)
            {
                return(BadRequest());
            }

            _context.Entry(tag).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TagExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddNote([FromBody] AddNoteDto note)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            IList <NoteTag> tags = new List <NoteTag>();

            if (note.Tags.Any())
            {
                foreach (var t in note.Tags)
                {
                    var tag = await _context.Tags.FirstOrDefaultAsync(x => x.Name.Equals(t, System.StringComparison.InvariantCultureIgnoreCase));

                    if (tag == null)
                    {
                        tag = new Tag {
                            Name = t
                        };
                    }
                    tags.Add(new NoteTag {
                        Tag = tag
                    });
                }
            }

            var newNote = new Note
            {
                Title = note.Title,
                Text  = note.Text,
                Tags  = tags
            };

            _context.Add(newNote);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetNote", new { id = newNote.Id }, note));
        }