public async Task RemoveCoureFromFavoritesAsync(CourseToFavoritesDto dto) { CranUser cranUser = await this.GetCranUserAsync(); RelUserCourseFavorite rel = await _context.RelUserCourseFavorites.Where(x => x.User.Id == cranUser.Id && x.Course.Id == dto.CourseId) .FirstOrDefaultAsync(); if (rel != null) { _context.Remove(rel); await this.SaveChangesAsync(); } }
public async Task AddCourseToFavoritesAsync(CourseToFavoritesDto dto) { CranUser cranUser = await this.GetCranUserAsync(); Course course = await _context.Courses.FindAsync(dto.CourseId); bool exists = await _context.RelUserCourseFavorites.AnyAsync(x => x.User.Id == cranUser.Id && x.Course.Id == course.Id); if (!exists) { RelUserCourseFavorite relUserCourseFavorite = new RelUserCourseFavorite() { User = cranUser, Course = course, }; _context.RelUserCourseFavorites.Add(relUserCourseFavorite); await SaveChangesAsync(); } }