# 12.2: Star API

## Add an API for the `IsStarred` option

In 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*

1. In the **ElevenNote.Web -> Controllers** folder, add a new folder called **WebAPI**
2. Add a **Controller** *(Web API 2 Controller - Empty)* named **NoteController**
   * This controller is **completely** different from the MVC `NoteController`
3. Add this code:

   ```csharp
    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);
        }
    }
   ```
4. `CTRL .` to bring in the using statements
5. Ignore the error on `IsStarred` for now, we'll fix that later

## Add `IsStarred` Property to `NoteEdit` Model

1. Go to **ElevenNote.Models -> NoteEdit.cs**
2. Add the `IsStarred` property.

   ```csharp
    public bool IsStarred { get; set; }
   ```
3. Go back to **ElevenNote.Web -> Controllers -> WebAPI -> NoteController.cs**
4. Notice that the error is gone
5. Run the app and test out the stars
6. If you refresh the page do the notes stay starred?

## Add `IsStarred` in the `UpdateNote` method

We need to save the star status and update this info in the database

1. Go to **ElevenNote.Services -> NoteService.cs**
2. Scroll down to the `UpdateNote` method and add the line starting with `entity.IsStarred...`

   ```csharp
            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;
            }
        }
   ```
3. Run the app and test out the star
4. Let's change the `Display(Name)` in the list view - **ElevenNote.Models -> NoteListItem.cs**

   ```csharp
        [UIHint("Starred")]
        [Display(Name = "Important")]
        public bool IsStarred { get; set; }
   ```
5. Run the app again and if it all works you can ![Git](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAwyv0-ttugakHO6zeV%2F-LAwyxUd0dcYSLXYb-5s%2Fdevicons_github_badge.png?generation=1524662787257488\&alt=media)
6. To see the `IsStarred` property change, open up your `dbo.Note` while the app is running. Refresh the table once you've selected/deselected the star.
7. If you've done this on a separate branch, now would probably be a good time to merge this into the other branch.

[Next,](https://eleven-fifty-academy.gitbook.io/dotnet-201-elevennote/star-part-12/part-12-add-star-option/broken-reference) we'll create a different API. This will be the API used for connecting to other presentation tiers in your application.
