Beispiel #1
0
 public async Task PostLike(int entryId, LikeRequest likeRequest)
 {
     UserInfo user = await GetUserInfoAsync();
     HttpStatusCode result = await TimelineOperations.AddOrRemoveTimelineEntryLikeAsync(
         DbContext, user.UserInfoId, entryId, likeRequest);
     result.ThrowHttpResponseExceptionIfNotSuccessful();
 }
 public async Task PostLike(int id, string extension, LikeRequest likeRequest)
 {
     UserInfo user = await GetUserInfoAsync();
     HttpStatusCode result = await UserMediaOperations.AddOrRemoveMediaLikeAsync(
         DbContext, user.UserInfoId, id, likeRequest);
     result.ThrowHttpResponseExceptionIfNotSuccessful();
 }
Beispiel #3
0
        public static async Task<HttpStatusCode> AddOrRemoveTimelineEntryLikeAsync(
            ApplicationDbContext dbContext,
            string userId,
            int entryId,
            LikeRequest like)
        {
            TimelineEntry entryEntity = await dbContext.TimelineEntries
                .SingleOrDefaultAsync(te => te.TimelineEntryId == entryId);
            if (entryEntity == null)
            {
                // The entry id is part of the URL, so return a 404.
                return HttpStatusCode.NotFound;
            }

            return await LikeOperations.AddOrRemoveLikeAsync(
                dbContext,
                userId,
                entryId,
                le => le.UserTextId,
                like);
        }
Beispiel #4
0
 public async Task PostLike(int commentId, LikeRequest likeRequest)
 {
     HttpStatusCode result = await CommentOperations.AddOrRemoveCommentLikeAsync(
         DbContext, UserId, commentId, likeRequest);
     result.ThrowHttpResponseExceptionIfNotSuccessful();
 }
Beispiel #5
0
        public static async Task<HttpStatusCode> AddOrRemoveAlbumLikeAsync(
            ApplicationDbContext dbContext,
            string userId,
            int entryId,
            LikeRequest like)
        {
            MediaAlbum albumEntity = await dbContext.MediaAlbums
                .SingleOrDefaultAsync(te => te.MediaAlbumId == entryId);
            if (albumEntity == null)
            {
                // The entry id is part of the URL, so return a 404.
                return HttpStatusCode.NotFound;
            }

            return await LikeOperations.AddOrRemoveLikeAsync(
                dbContext,
                userId,
                entryId,
                le => le.MediaAlbumId,
                like);
        }
Beispiel #6
0
 public async Task PostLikeAlbum(int albumId, LikeRequest likeRequest)
 {
     HttpStatusCode result = await AlbumOperations.AddOrRemoveAlbumLikeAsync(
         DbContext, UserId, albumId, likeRequest);
     result.ThrowHttpResponseExceptionIfNotSuccessful();
 }
Beispiel #7
0
        public static async Task<HttpStatusCode> AddOrRemoveLikeAsync(
            ApplicationDbContext dbContext,
            string userId,
            int likedId,
            Expression<Func<Like, int?>> entryIdProperty,
            LikeRequest like)
        {            
            using (var transaction = dbContext.Database.BeginTransaction())
            {
                bool succeeded = false;
                try
                {
                    LikeKind kind;
                    switch (like.LikeKind.ToLowerInvariant())
                    {
                        case "like":
                            kind = LikeKind.Like;
                            break;

                        case "frown":
                            kind = LikeKind.Frown;
                            break;

                        case "hug":
                            kind = LikeKind.Hug;
                            break;

                        default:
                            return HttpStatusCode.BadRequest;
                    }

                    var match = (Expression<Func<Like, bool>>) Expression.Lambda(
                        Expression.Equal(
                            entryIdProperty.Body,
                            Expression.Convert(
                                Expression.Constant(likedId),
                                typeof(int?))),
                        entryIdProperty.Parameters[0]);

                    // Using First rather than Single because there are race conditions in which
                    // we can end up with multiple likes.
                    Like existingLikeEntity = await dbContext.Likes
                        .Where(le => le.Kind == kind)
                        .FirstOrDefaultAsync(match);

                    HttpStatusCode result;
                    if (like.Set)
                    {
                        if (existingLikeEntity != null)
                        {
                            // Ignore a relike
                            result = HttpStatusCode.OK;
                        }
                        var idProp = (PropertyInfo) ((MemberExpression) entryIdProperty.Body).Member;
                        result = await AddLikeAsync(dbContext, userId, idProp, likedId, kind);
                    }
                    else
                    {
                        if (existingLikeEntity != null)
                        {
                            dbContext.Likes.Remove(existingLikeEntity);
                            await dbContext.SaveChangesAsync();
                        } // Ignore a reunlike
                        result = HttpStatusCode.OK;
                    }
                    transaction.Commit();
                    succeeded = true;
                    return result;
                }
                finally
                {
                    if (!succeeded)
                    {
                        transaction.Rollback();
                    }
                }
            }
        }