public void InsertOrUpdate(Like like)
 {
     if (like.LikeId == default(int)) {
         // New entity
         context.Likes.Add(like);
     } else {
         // Existing entity
         context.Entry(like).State = EntityState.Modified;
     }
 }
 public ActionResult Create(Like like)
 {
     if (ModelState.IsValid) {
         likeRepository.InsertOrUpdate(like);
         likeRepository.Save();
         return RedirectToAction("Index");
     } else {
         ViewBag.PossibleTracks = trackRepository.All;
         return View();
     }
 }
 private void SaveRepository(Statistic statistic, Like like, Track track)
 {
     trackRepository.InsertOrUpdate(track);
     trackRepository.Save();
     likeRepository.Save();
     _statisticRepository.InsertOrUpdate(statistic);
     _statisticRepository.Save();
 }
 public JsonResult Like(int trackId)
 {
     var likes = trackR.CurrentLikes(trackId);
     Track track = trackRepository.Find(trackId);
     var statistic = _statisticRepository.CurrentStatistics(WebSecurity.CurrentUserId, date);
     if (statistic == null)
     {
         CreateNewStatistic(statistic);
     }
     if (likes.Count() == 0)
     {
         track.Like += 1;
         Like like = new Like();
         likeRepository.InsertOrUpdate(like);
         like.TrackId = trackId;
         like.UserName = WebSecurity.CurrentUserName;
         statistic.CountLikes += 1;
         SaveRepository(statistic, like, track);
     }
     else
     {
         var singleOrDefault = likes.SingleOrDefault(t => t.UserName == WebSecurity.CurrentUserName);
         if (singleOrDefault != null)
         {
             track.Like -= 1;
             int likeId = singleOrDefault.LikeId;
             likeRepository.Delete(likeId);
             SaveRepository(statistic, null, track);
         }
     }
     GoLucene.ClearLuceneIndex();
     GoLucene.AddUpdateLuceneIndex(TrackDataRepository.GetAll());
     return Json(track.Like);
 }