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))); }
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))); } }
// [OutputCache(Duration = 3600, VaryByParam = "*")] public async Task <object> GetTimeline(int id) { dynamic cResponse = new ExpandoObject(); try { // API'nin dondurecegi liste List <TimelineViewModel> TimelineViewModelList = new List <TimelineViewModel>(); // takip listesi icin gerekli List <PersonFollowing> dbPFollowingList = await db.PersonFollowing.Where(x => x.PersonID == id && x.IsAccepted == true).ToListAsync(); // takip ettigi kisilerin ve kendi id'sinin bulundugu ID listesi List <int> SPersonIDList = new List <int>(); // kendi olusturdugu timeline'in gelmesi icin kisinin id si eklenir. SPersonIDList.Add(id); // takip ettigi arkadaslarinin da aktivitelerini getirmek icin ID listesi hazirlanir foreach (PersonFollowing item in dbPFollowingList) { SPersonIDList.Add(item.SecondaryPersonID); } // Tum ID filtreli ve eklenme zamanina gore ters siralandirilmis Timeline listesi List <Timeline> dbTimelineList = await db.Timeline.Where(x => SPersonIDList.Contains(x.PersonID)).OrderByDescending(x => x.FeedDate).ToListAsync(); // Her Timeline icin -> TimelineViewModel Olusturur foreach (Timeline itemTimeline in dbTimelineList) { // Siradaki Timeline icin Timeline'i olusturan kisiyi getirir Person dbPerson = await db.Person.Where(x => x.PersonID == itemTimeline.PersonID).SingleOrDefaultAsync(); TimelineViewModel tModel = new TimelineViewModel(); tModel.TimelineID = itemTimeline.TimelineID; tModel.StoryID = itemTimeline.StoryID; tModel.PersonID = itemTimeline.PersonID; tModel.PersonThumbnail = dbPerson.PhotoUrlThumbnail; tModel.PersonUsername = dbPerson.Username; tModel.DateFeed = itemTimeline.FeedDate; tModel.IsReTell = itemTimeline.IsReTell; tModel.CoverPhotoIndex = itemTimeline.CoverPhotoIndex; // Her Timeline icin -> ilgili story getirili Story dbStory = await db.Story.Where(x => x.StoryID == itemTimeline.StoryID).SingleOrDefaultAsync(); StoryViewModel sModel = new StoryViewModel(); sModel.StoryID = itemTimeline.StoryID; sModel.ThemeID = dbStory.ThemeID; sModel.Tag = dbStory.Tag; CountStory dbCountStory = await db.CountStory.Where(x => x.StoryID == itemTimeline.StoryID).SingleOrDefaultAsync(); if (dbCountStory != null) { sModel.CountComment = dbCountStory.Comment; } else { sModel.CountComment = 0; } List <MomentViewModel> momentViewModelList = new List <MomentViewModel>(); List <int> MomentIDList = new List <int>(); MomentIDList.Add(dbStory.MomentID1); MomentIDList.Add(dbStory.MomentID2); if (dbStory.MomentID3 != null) { MomentIDList.Add(Convert.ToInt32(dbStory.MomentID3)); } if (dbStory.MomentID4 != null) { MomentIDList.Add(Convert.ToInt32(dbStory.MomentID4)); } if (dbStory.MomentID5 != null) { MomentIDList.Add(Convert.ToInt32(dbStory.MomentID5)); } if (dbStory.MomentID6 != null) { MomentIDList.Add(Convert.ToInt32(dbStory.MomentID6)); } if (dbStory.MomentID7 != null) { MomentIDList.Add(Convert.ToInt32(dbStory.MomentID7)); } foreach (int item in MomentIDList) { Moment dbMoment = await db.Moment.Where(x => x.MomentID == item).SingleOrDefaultAsync(); MomentViewModel mModel = new MomentViewModel(); mModel.MomentID = item; mModel.PhotoUrlLarge = dbMoment.PhotoUrlLarge; mModel.PersonID = dbMoment.PersonID; mModel.Title = dbMoment.Title; mModel.IsHorizontal = dbMoment.IsHorizontal; mModel.LocationString = dbMoment.LocationString; CountMoment dbCountMoment = await db.CountMoment.Where(x => x.MomentID == item).SingleOrDefaultAsync(); if (dbCountMoment != null) { mModel.CountLike = dbCountMoment.LikeCount; } else { mModel.CountLike = 0; } momentViewModelList.Add(mModel); } // story'e ait moment'lar storymodel e eklenir. sModel.MomentList = momentViewModelList; // ekleme islemleri tModel.StoryViewModel = sModel; TimelineViewModelList.Add(tModel); } cResponse.Result = "0"; cResponse.Description = "All Timeline"; cResponse.DateNow = DateTime.Now; cResponse.Timeline = TimelineViewModelList; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } catch (Exception ex) { cResponse.Result = "-1"; cResponse.Description = "Your request could not executed"; return(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse))); } }