public async Task <IActionResult> Post([FromBody] Star follow) { // We need a user to subscribe to the thing var user = await base.GetAuthenticatedUserAsync(); if (user == null) { return(base.UnauthorizedException()); } // Has the user already starred the thing? var existingStar = await _starStore.SelectByNameThingIdAndCreatedUserId( follow.Name, follow.ThingId, user.Id); if (existingStar != null) { return(base.Result(HttpStatusCode.OK, $"Authenticated user already stared object with id '{follow.ThingId}'")); } // Build a new subscription var starToAdd = new Star() { Name = follow.Name, ThingId = follow.ThingId, CreatedUserId = user.Id, CreatedDate = DateTime.UtcNow }; // Add and return result var result = await _starManager.CreateAsync(starToAdd); if (result != null) { // Award reputation return(base.Result(result)); } // We should not reach here return(base.InternalServerError()); }
public override async Task <IViewProviderResult> BuildDisplayAsync(Doc entity, IViewProviderContext updater) { if (entity == null) { return(await BuildIndexAsync(new Doc(), updater)); } var isStarred = false; var starType = StarTypes.Doc; var user = await _contextFacade.GetAuthenticatedUserAsync(); if (user != null) { var entityStar = await _starStore.SelectByNameThingIdAndCreatedUserId( starType.Name, entity.Id, user.Id); if (entityStar != null) { isStarred = true; } } return(Views( View <StarViewModel>("Star.Display.Tools", model => { model.StarType = starType; model.ThingId = entity.Id; model.IsStarred = isStarred; model.TotalStars = entity.TotalStars; model.Permission = Permissions.StarDocs; return model; }).Zone("tools").Order(-5) )); }