Exemplo n.º 1
0
        public async Task <object> PostMomentLike(MomentLike momentLike)
        {
            dynamic cResponse = new ExpandoObject();

            if (!ModelState.IsValid)
            {
                cResponse.Result      = "-1";
                cResponse.Description = ModelState;
                return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse)));
            }

            int resultLike = await db.MomentLike.CountAsync(x => x.PersonID == momentLike.PersonID && x.MomentID == momentLike.MomentID);

            if (resultLike > 0)
            {
                cResponse.Result      = "-1";
                cResponse.Description = "Already Liked";
                return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse)));
            }

            momentLike.LikeDate = DateTime.Now;

            db.MomentLike.Add(momentLike);
            await db.SaveChangesAsync();

            CountMoment dbCountMoment = await db.CountMoment.Where(x => x.MomentID == momentLike.MomentID).SingleOrDefaultAsync();

            if (dbCountMoment != null)
            {
                dbCountMoment.LastActivityDate = DateTime.Now;
                dbCountMoment.LikeCount        = dbCountMoment.LikeCount + 1;
                await db.SaveChangesAsync();
            }
            else
            {
                CountMoment newCountMoment = new CountMoment();
                newCountMoment.LastActivityDate = DateTime.Now;
                newCountMoment.MomentID         = momentLike.MomentID;
                newCountMoment.LikeCount        = 1;
                await db.SaveChangesAsync();
            }

            cResponse.Result      = "0";
            cResponse.Description = "Like added to database";
            return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse)));
        }
Exemplo n.º 2
0
        public async Task <object> PostMomentLike(MomentLike like)
        {
            List <MomentLike> likes = await db.MomentLike.Where(p => p.MomentID == like.MomentID).ToListAsync();

            List <int> IDList = new List <int>();

            foreach (MomentLike item in likes)
            {
                IDList.Add(item.PersonID);
            }

            List <Person> people = await db.Person.Where(t => IDList.Contains(t.PersonID)).ToListAsync();

            List <MomentLikeViewModel> likeModels = new List <MomentLikeViewModel>();

            foreach (Person item in people)
            {
                MomentLikeViewModel model = new MomentLikeViewModel();
                model.MomentID                = like.MomentID;
                model.PersonID                = item.PersonID;
                model.PersonFirstName         = item.FirstName;
                model.PersonLastName          = item.LastName;
                model.PersonPhotoUrlThumbnail = item.PhotoUrlThumbnail;
                model.PersonUsername          = item.Username;

                int result = await db.PersonFollowing.CountAsync(x => x.PersonID == like.PersonID && x.SecondaryPersonID == item.PersonID && x.IsAccepted == true);

                if (result > 0)
                {
                    model.isFollowing = true;
                }
                likeModels.Add(model);
            }

            dynamic cResponse = new ExpandoObject();

            cResponse.Result  = "0";
            cResponse.DateNow = DateTime.Now;
            cResponse.Data    = likeModels;
            return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse)));
        }
        public async Task <object> PostMomentLike(MomentLike like)
        {
            dynamic cResponse = new ExpandoObject();

            try
            {
                MomentLike mLike = await db.MomentLike.Where(x => x.MomentID == like.MomentID && x.PersonID == like.PersonID).SingleOrDefaultAsync();

                if (mLike != null)
                {
                    db.MomentLike.Remove(mLike);
                    await db.SaveChangesAsync();

                    CountMoment mMoment = await db.CountMoment.FindAsync(like.MomentID);

                    if (mMoment != null)
                    {
                        mMoment.LastActivityDate = DateTime.Now;
                        mMoment.LikeCount        = mMoment.LikeCount - 1;
                        await db.SaveChangesAsync();
                    }

                    cResponse.Result      = "0";
                    cResponse.Description = "you unliked";
                    return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse)));
                }
                else
                {
                    cResponse.Result      = "0";
                    cResponse.Description = "you unliked";
                    return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse)));
                }
            }
            catch (Exception ex)
            {
                cResponse.Result      = "0";
                cResponse.Description = "Exception, your request could not be executed";
                return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse)));
            }
        }