School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,172 @@
using TechTitans.Models;
using TechTitans.Repositories;
namespace TechTitans.Services
{
public class ArtistSongDashboardController
{
private Repository<SongBasicDetails> SongRepo = new Repository<SongBasicDetails>();
private Repository<SongFeatures> FeatureRepo = new Repository<SongFeatures>();
private Repository<SongRecommendationDetails> SongRecommendationRepo = new Repository<SongRecommendationDetails>();
private Repository<AuthorDetails> ArtistRepo = new Repository<AuthorDetails>();
//converts song details to song info
public SongBasicInfo songBasicDetailsToInfo(SongBasicDetails song)
{
SongBasicInfo songInfo = new SongBasicInfo();
songInfo.SongId = song.Song_Id;
songInfo.Name = song.Name;
songInfo.Genre = song.Genre;
songInfo.Subgenre = song.Subgenre;
songInfo.Language = song.Language;
songInfo.Country = song.Country;
songInfo.Album = song.Album;
songInfo.Image = song.Image;
foreach (AuthorDetails artist in ArtistRepo.GetAll())
{
if (artist.Artist_Id == song.Artist_Id)
{
songInfo.Artist = artist.Name;
}
}
foreach (SongFeatures feature in FeatureRepo.GetAll())
{
if (feature.Song_Id == song.Song_Id)
{
songInfo.Features.Add(feature.ToString());
}
}
return songInfo;
}
//get all artist's publish song (returns List<Entity>)
public List<SongBasicInfo> getAllArtistSongs(int artistId)
{
List<SongBasicInfo> artistSongs = new List<SongBasicInfo>();
foreach (SongBasicDetails song in SongRepo.GetAll())
{
if (song.Artist_Id == artistId)
{
SongBasicInfo songInfo = songBasicDetailsToInfo(song);
artistSongs.Add(songInfo);
}
}
return artistSongs;
}
//search by string in titles (returns list of songs, case insensitive)
public List<SongBasicInfo> searchByTitle(string title)
{
List<SongBasicInfo> songs = new List<SongBasicInfo>();
foreach (SongBasicDetails song in SongRepo.GetAll())
{
if (song.Name.ToLower().Trim().Contains(title.ToLower()))
{
SongBasicInfo songInfo = songBasicDetailsToInfo(song);
songs.Add(songInfo);
}
}
return songs;
}
//gets song info by song id
public SongBasicInfo getSongInfo(int songId)
{
foreach (SongBasicDetails song in SongRepo.GetAll())
{
if (song.Song_Id == songId)
{
SongBasicInfo songInfo = songBasicDetailsToInfo(song);
return songInfo;
}
}
return null;
}
//gets song recommendation details by song id
public SongRecommendationDetails getSongDetails(int songId)
{
foreach (SongRecommendationDetails songDetails in SongRecommendationRepo.GetAll())
{
if (songDetails.Song_Id == songId)
{
return songDetails;
}
}
return new SongRecommendationDetails();
}
//gets artist info by song id
public AuthorDetails getArtistInfo(int SongId)
{
foreach (SongBasicDetails song in SongRepo.GetAll())
{
if (song.Song_Id == SongId)
{
foreach (AuthorDetails artist in ArtistRepo.GetAll())
{
if (artist.Artist_Id == song.Artist_Id)
{
return artist;
}
}
}
}
return null;
}
public AuthorDetails getMostPublishedAuthor()
{
Dictionary<int, int> artistCount = new Dictionary<int, int>();
foreach (SongBasicDetails song in SongRepo.GetAll())
{
if (artistCount.ContainsKey(song.Artist_Id))
{
artistCount[song.Artist_Id]++;
}
else
{
artistCount.Add(song.Artist_Id, 1);
}
}
int max = 0;
int artistId = 0;
foreach (KeyValuePair<int, int> entry in artistCount)
{
if (entry.Value > max)
{
max = entry.Value;
artistId = entry.Key;
}
}
foreach (AuthorDetails artist in ArtistRepo.GetAll())
{
if (artist.Artist_Id == artistId)
{
return artist;
}
}
return null;
}
public List<SongBasicInfo> getSongsForMainPage()
{
List<SongBasicInfo> songs = new List<SongBasicInfo>();
int artistId = getMostPublishedAuthor().Artist_Id;
foreach (SongBasicDetails song in SongRepo.GetAll())
{
if (song.Artist_Id == artistId)
{
SongBasicInfo songInfo = songBasicDetailsToInfo(song);
songs.Add(songInfo);
}
}
return songs;
}
}
}
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using System.Windows.Forms;
using TechTitans.Models;
using TechTitans.Repositories;
using TechTitans.Enums;
namespace TechTitans.Services
{
internal class FullDetailsOnSongController
{
//private readonly Repository<SongBasicDetails> SongRepo = new();
//private readonly Repository<SongRecommendationDetails> SongRecommendationRepo = new();
private readonly Repository<UserPlaybackBehaviour> UserPlaybackBehaviourRepo = new();
private readonly Repository<AdDistributionData> AdDistributionDataRepo = new();
public FullDetailsOnSong GetFullDetailsOnSong(int songId) {
FullDetailsOnSong currentSongDetails = new();
DateTime start = new();
bool found = false;
foreach (UserPlaybackBehaviour action in UserPlaybackBehaviourRepo.GetAll()) {
if (action.Song_Id == songId) {
found = true;
// string message;
// message = action.Event_Type.ToString() + " " + action.Timestamp.ToString() + " " + action.Song_Id.ToString() + " " + action.User_Id.ToString() + "\n";
// MessageBox.Show(message);
switch (action.Event_Type) {
case PlaybackEventType.start_play:
start = action.Timestamp;
break;
case PlaybackEventType.end_play:
int minutes = (action.Timestamp - start).Minutes;
currentSongDetails.TotalMinutesListened += minutes;
currentSongDetails.TotalPlays++;
break;
case PlaybackEventType.like:
currentSongDetails.TotalLikes++;
break;
case PlaybackEventType.dislike:
currentSongDetails.TotalDislikes++;
break;
case PlaybackEventType.skip:
currentSongDetails.TotalSkips++;
break;
}
}
}
// MessageBox.Show(currentSongDetails.TotalMinutesListened.ToString());
if (!found) {
// MessageBox.Show("Song not found");
return null;
}
return currentSongDetails;
}
public FullDetailsOnSong GetCurrentMonthDetails(int songId) {
FullDetailsOnSong currentSongDetails = new();
foreach (UserPlaybackBehaviour action in UserPlaybackBehaviourRepo.GetAll()) {
if (action.Song_Id == songId && action.Timestamp.Month == DateTime.Now.Month && action.Timestamp.Year == DateTime.Now.Year) {
switch (action.Event_Type) {
case PlaybackEventType.start_play:
break;
case PlaybackEventType.end_play:
int minutes = (action.Timestamp - DateTime.Now).Minutes;
currentSongDetails.TotalMinutesListened += minutes;
currentSongDetails.TotalPlays++;
break;
case PlaybackEventType.like:
currentSongDetails.TotalLikes++;
break;
case PlaybackEventType.dislike:
currentSongDetails.TotalDislikes++;
break;
case PlaybackEventType.skip:
currentSongDetails.TotalSkips++;
break;
}
}
}
return currentSongDetails;
}
public AdDistributionData GetActiveAd(int songId) {
foreach (AdDistributionData ad in AdDistributionDataRepo.GetAll()) {
if (ad.Song_Id == songId && ad.Month == DateTime.Now.Month && ad.Year == DateTime.Now.Year) {
return ad;
}
}
return null;
}
}
}
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTitans.Repositories;
using TechTitans.Models;
using Dapper;
using TechTitans.ViewModels;
using TechTitans.Enums;
namespace TechTitans.Services
{
internal class RecapService
{
SongBasicDetailsRepository songBasicDetailsRepository = new SongBasicDetailsRepository();
UserPlaybackBehaviourRepository userPlaybackBehaviourRepository = new UserPlaybackBehaviourRepository();
public List<SongBasicInfo> Top5MostListenedSongs(int userId)
{
var top5Songs = songBasicDetailsRepository.GetTop5MostListenedSongs(userId);
List<SongBasicInfo> top5SongsInfo = new List<SongBasicInfo>();
foreach (var song in top5Songs)
{
top5SongsInfo.Add(songBasicDetailsRepository.SongBasicDetailsToSongBasicInfo(song));
}
return top5SongsInfo;
}
public Tuple<SongBasicInfo, decimal> MostPlayedSongPercentile(int userId)
{
var mostPlayedSong = songBasicDetailsRepository.GetMostPlayedSongPercentile(userId);
return new Tuple<SongBasicInfo, decimal>(songBasicDetailsRepository.SongBasicDetailsToSongBasicInfo(mostPlayedSong.Item1), mostPlayedSong.Item2);
}
public Tuple<string, decimal> MostPlayedArtistPercentile(int userId)
{
return songBasicDetailsRepository.GetMostPlayedArtistPercentile(userId);
}
public int MinutesListened(int userId)
{
//get list of all userplaybackbehaviour for a userid sorted ascending by timestamp and go over them calculating the difference between the timestamps and summing them up
var userEvents = userPlaybackBehaviourRepository.GetUserPlaybackBehaviour(userId);
int minutesListened = 0;
//for every start and end event pair, calculate the difference in minutes and add it to the total
for (int i = 0; i < userEvents.Count; i++)
{
if (userEvents[i].Event_Type == PlaybackEventType.start_play)
{
for (int j = i + 1; j < userEvents.Count; j++)
{
if (userEvents[j].Event_Type == PlaybackEventType.end_play)
{
minutesListened += (int)(userEvents[j].Timestamp - userEvents[i].Timestamp).TotalMinutes;
i = j;
break;
}
}
}
}
return minutesListened;
}
public List<string> Top5Genres(int userId)
{
return this.songBasicDetailsRepository.GetTop5Genres(userId);
}
public List<string> NewGenresDiscovered(int userId)
{
return this.songBasicDetailsRepository.NewGenresDiscovered(userId);
}
public ListenerPersonality ListenerPersonality(int userId)
{
var userEvents = userPlaybackBehaviourRepository.GetUserPlaybackBehaviour(userId);
int playCount = 0;
for(int i = 0; i < userEvents.Count; i++)
{
if ((userEvents[i].Event_Type == PlaybackEventType.start_play) && (userEvents[i].Timestamp.Year == DateTime.Now.Year))
{
playCount++;
}
}
if(playCount > 100)
{
return Enums.ListenerPersonality.Melophile;
}
var newGenres = NewGenresDiscovered(userId);
if(newGenres.Count > 3)
{
return Enums.ListenerPersonality.Explorer;
}
if(playCount < 10)
{
return Enums.ListenerPersonality.Casual;
}
return Enums.ListenerPersonality.Vanilla;
}
public EndOfYearRecapViewModel GenerateEndOfYearRecap(int userId)
{
var recap = new EndOfYearRecapViewModel();
recap.Top5MostListenedSongs = Top5MostListenedSongs(userId);
recap.MostPlayedSongPercentile = MostPlayedSongPercentile(userId);
recap.MostPlayedArtistPercentile = MostPlayedArtistPercentile(userId);
recap.MinutesListened = MinutesListened(userId);
recap.Top5Genres = Top5Genres(userId);
recap.NewGenresDiscovered = NewGenresDiscovered(userId);
recap.ListenerPersonality = ListenerPersonality(userId);
return recap;
}
}
}
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTitans.Models;
using TechTitans.Repositories;
namespace TechTitans.Services
{
public class TopGenresController
{
private Repository<SongBasicDetails> SongRepo = new Repository<SongBasicDetails>();
private Repository<SongRecommendationDetails> SongRecommendationRepo= new Repository<SongRecommendationDetails>();
public void getTop3Genres(int month,int year,Label genre1,Label minutes1,Label percentage1 , Label genre2, Label minutes2,Label percentage2, Label genre3 ,Label minutes3, Label percentage3) {
int totalMinutes = 0;
Dictionary<String, int> genreCount = new Dictionary<String, int>();
foreach (SongBasicDetails song in SongRepo.GetAll()) {
foreach (SongRecommendationDetails songDetails in SongRecommendationRepo.GetAll())
{
if (songDetails.Song_Id == song.Song_Id && songDetails.Month == month && songDetails.Year == year)
{
totalMinutes += songDetails.Minutes_Listened;
if (genreCount.ContainsKey(song.Genre))
{
genreCount[song.Genre] += songDetails.Minutes_Listened;
}
else
{
genreCount.Add(song.Genre, songDetails.Minutes_Listened);
}
}
}
}
var sortedDict = from entry in genreCount orderby entry.Value descending select entry;
int count = 0;
foreach (KeyValuePair<String, int> entry in sortedDict)
{
switch(count)
{
case 0:
genre1.Text = entry.Key;
minutes1.Text = entry.Value.ToString();
percentage1.Text = ((entry.Value / totalMinutes) * 100).ToString();
break;
case 1:
genre2.Text = entry.Key;
minutes2.Text = entry.Value.ToString();
percentage2.Text = ((entry.Value / totalMinutes) * 100).ToString();
break;
case 2:
genre3.Text = entry.Key;
minutes3.Text = entry.Value.ToString();
percentage3.Text = ((entry.Value / totalMinutes) * 100).ToString();
break;
}
if(count ==2)
{
break;
}
count++;
}
}
public void top3SubGenres(int month, int year, Label genre1, Label minutes1, Label percentage1, Label genre2, Label minutes2, Label percentage2, Label genre3, Label minutes3, Label percentage3) {
Dictionary<String, int> subgenreCount = new Dictionary<String, int>();
int totalMinutes = 0;
foreach (SongBasicDetails song in SongRepo.GetAll())
{
foreach (SongRecommendationDetails songDetails in SongRecommendationRepo.GetAll())
{
if (songDetails.Song_Id == song.Song_Id && songDetails.Month == month && songDetails.Year == year)
{
totalMinutes += songDetails.Minutes_Listened;
if (subgenreCount.ContainsKey(song.Subgenre))
{
subgenreCount[song.Subgenre] += songDetails.Minutes_Listened;
}
else
{
subgenreCount.Add(song.Subgenre, songDetails.Minutes_Listened);
}
}
}
}
var sortedDict = from entry in subgenreCount orderby entry.Value descending select entry;
int count = 0;
foreach (KeyValuePair<String, int> entry in sortedDict)
{
switch (count)
{
case 0:
genre1.Text = entry.Key;
minutes1.Text = entry.Value.ToString();
percentage1.Text = ((entry.Value / totalMinutes) * 100).ToString();
break;
case 1:
genre2.Text = entry.Key;
minutes2.Text = entry.Value.ToString();
percentage2.Text = ((entry.Value / totalMinutes) * 100).ToString();
break;
case 2:
genre3.Text = entry.Key;
minutes3.Text = entry.Value.ToString();
percentage3.Text = ((entry.Value / totalMinutes) * 100).ToString();
break;
}
if (count == 2)
{
break;
}
count++;
count++;
}
}
}
}
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTitans.Models;
using TechTitans.Repositories;
namespace TechTitans.Services
{
internal class UserService
{
private UserRepository SongRepo= new UserRepository();
public List<SongBasicInfo> get_recently_played() {
List<SongBasicInfo> list_song=new List<SongBasicInfo>();
foreach (SongBasicDetails song in SongRepo.GetAll()){
SongBasicInfo song_info = SongRepo.SongBasicDetailsToSongBasicInfo(song);
list_song.Add(song_info);
}
return list_song.Take(6).ToList();
}
}
}