Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- public IActionResult PostNote([FromBody] Note note)
- {
- if (!ModelState.IsValid)
- {
- return HttpBadRequest(ModelState);
- }
- string[] separators = { " " };
- List<string> tags = note.TagsAsSingleString.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToList();
- foreach(var t in tags)
- {
- Tag tagInDb = db.Tags.FirstOrDefault(x => x.Name == t);
- if(tagInDb == null)
- {
- Tag newTag = new Tag { Name = t };
- db.Tags.Add(newTag);
- }
- }
- db.Notes.Add(note);
- try
- {
- db.SaveChanges();
- }
- catch (DbUpdateException)
- {
- if (NoteExists(note.ID))
- {
- return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
- }
- else
- {
- throw;
- }
- }
- return CreatedAtRoute("GetNote", new { id = note.ID }, note);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement