12.2: Star API
Add an API for the IsStarred option
IsStarred optionIn this section, we will add an API specifically for our star functionality. Please note: This is different from the API we will use later for working with other presentation tiers
In the ElevenNote.Web -> Controllers folder, add a new folder called WebAPI
Add a Controller (Web API 2 Controller - Empty) named NoteController
This controller is completely different from the MVC
NoteController
Add this code:
namespace ElevenNote.Web.Controllers.WebAPI { [Authorize] [RoutePrefix("api/Note")] public class NoteController : ApiController { private bool SetStarState(int noteId, bool newState) { // Create the service var userId = Guid.Parse(User.Identity.GetUserId()); var service = new NoteService(userId); // Get the note var detail = service.GetNoteById(noteId); // Create the NoteEdit model instance with the new star state var updatedNote = new NoteEdit { NoteId = detail.NoteId, Title = detail.Title, Content = detail.Content, IsStarred = newState }; // Return a value indicating whether the update succeeded return service.UpdateNote(updatedNote); } [Route("{id}/Star")] [HttpPut] public bool ToggleStarOn(int id) => SetStarState(id, true); [Route("{id}/Star")] [HttpDelete] public bool ToggleStarOff(int id) => SetStarState(id, false); } }CTRL .to bring in the using statementsIgnore the error on
IsStarredfor now, we'll fix that later
Add IsStarred Property to NoteEdit Model
IsStarred Property to NoteEdit ModelGo to ElevenNote.Models -> NoteEdit.cs
Add the
IsStarredproperty.public bool IsStarred { get; set; }Go back to ElevenNote.Web -> Controllers -> WebAPI -> NoteController.cs
Notice that the error is gone
Run the app and test out the stars
If you refresh the page do the notes stay starred?
Add IsStarred in the UpdateNote method
IsStarred in the UpdateNote methodWe need to save the star status and update this info in the database
Go to ElevenNote.Services -> NoteService.cs
Scroll down to the
UpdateNotemethod and add the line starting withentity.IsStarred...public bool UpdateNote(NoteEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Notes .Single(e => e.NoteId == model.NoteId && e.OwnerId == _userId); entity.Title = model.Title; entity.Content = model.Content; entity.ModifiedUtc = DateTimeOffset.UtcNow; entity.IsStarred = model.IsStarred; return ctx.SaveChanges() == 1; } }Run the app and test out the star
Let's change the
Display(Name)in the list view - ElevenNote.Models -> NoteListItem.cs[UIHint("Starred")] [Display(Name = "Important")] public bool IsStarred { get; set; }Run the app again and if it all works you can

To see the
IsStarredproperty change, open up yourdbo.Notewhile the app is running. Refresh the table once you've selected/deselected the star.If you've done this on a separate branch, now would probably be a good time to merge this into the other branch.
Next, we'll create a different API. This will be the API used for connecting to other presentation tiers in your application.
Last updated