Advertisement
Ludwiq

Untitled

Dec 30th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. [HttpPost]
  2.         public IActionResult PostNote([FromBody] Note note)
  3.         {
  4.             if (!ModelState.IsValid)
  5.             {
  6.                 return HttpBadRequest(ModelState);
  7.             }
  8.  
  9.             string[] separators = { " " };
  10.             List<string> tags = note.TagsAsSingleString.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToList();
  11.             foreach(var t in tags)
  12.             {
  13.                 Tag tagInDb = db.Tags.FirstOrDefault(x => x.Name == t);
  14.  
  15.                 if(tagInDb == null)
  16.                 {
  17.                     Tag newTag = new Tag { Name = t };
  18.                     db.Tags.Add(newTag);
  19.                    
  20.                 }
  21.             }
  22.  
  23.             db.Notes.Add(note);
  24.             try
  25.             {
  26.                 db.SaveChanges();
  27.             }
  28.             catch (DbUpdateException)
  29.             {
  30.                 if (NoteExists(note.ID))
  31.                 {
  32.                     return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
  33.                 }
  34.                 else
  35.                 {
  36.                     throw;
  37.                 }
  38.             }
  39.  
  40.             return CreatedAtRoute("GetNote", new { id = note.ID }, note);
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement