Beispiel #1
0
        public async Task <bool> Like(int musicId, int userId, Like like)
        {
            var music = await _musicRepository.GetByIdAsync(musicId);

            var user = await _detailRepository.GetByIdAsync(userId);

            var control = await _likeRepository.LikeControl(userId, musicId);

            if (control)//true
            {
                var deleteLike = _context.Likes.FirstOrDefaultAsync(x => x.UserId == userId && x.MusicId == musicId).Result;
                _likeRepository.Remove(deleteLike);
                music.TotalLikes--;
                user.TotalLikes--;
                user.TotalScore += _detailRepository.GiveRandomScore(2);
                await _context.SaveChangesAsync();

                return(true);
            }
            else//false
            {
                await _likeRepository.AddAsync(like);

                music.TotalLikes++;
                user.TotalLikes++;
                user.TotalScore += _detailRepository.GiveRandomScore();
                await _context.SaveChangesAsync();

                return(false);
            }
        }
Beispiel #2
0
 public async Task CommitAsync()
 {
     await _context.SaveChangesAsync();
 }