120 lines
3.5 KiB
C#
120 lines
3.5 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
namespace back.DataModels;
|
|
|
|
public class RankingGroup
|
|
{
|
|
public const float ExtremlyBad = 1/5f;
|
|
public const float Bad = 2 / 5f;
|
|
public const float Normal = 3 / 5f;
|
|
public const float Good = 4 / 5f;
|
|
public const float ExtremlyGood = 5 / 5f;
|
|
|
|
public static string GetGroup(float score) => (float)Math.Ceiling(score) switch
|
|
{
|
|
<= ExtremlyBad => nameof(ExtremlyBad),
|
|
<= Bad => nameof(Bad),
|
|
<= Normal => nameof(Normal),
|
|
<= Good => nameof(Good),
|
|
_ => nameof(ExtremlyGood)
|
|
};
|
|
}
|
|
|
|
[Table("Rankings")]
|
|
public partial class Ranking : IEquatable<Ranking>
|
|
{
|
|
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
|
public string Id { get; set; } = null!;
|
|
public int TotalVotes { get; set; }
|
|
public int UpVotes { get; set; }
|
|
public int DownVotes { get; set; }
|
|
|
|
public Ranking(int totalVotes, int upVotes = 0, int downVotes = 0)
|
|
{
|
|
TotalVotes = totalVotes;
|
|
UpVotes = upVotes;
|
|
DownVotes = downVotes;
|
|
}
|
|
|
|
public Ranking()
|
|
{
|
|
TotalVotes = 0;
|
|
UpVotes = 0;
|
|
DownVotes = 0;
|
|
}
|
|
|
|
public void DownVote()
|
|
{
|
|
DownVotes++;
|
|
TotalVotes++;
|
|
}
|
|
|
|
public void UpVote()
|
|
{
|
|
UpVotes++;
|
|
TotalVotes++;
|
|
}
|
|
|
|
public float Score
|
|
{
|
|
get
|
|
{
|
|
if (TotalVotes == 0) return 0;
|
|
return (float)(UpVotes - DownVotes) / TotalVotes;
|
|
}
|
|
}
|
|
|
|
public string Group => RankingGroup.GetGroup(Score);
|
|
|
|
public override int GetHashCode() => HashCode.Combine(Id, TotalVotes, UpVotes, DownVotes);
|
|
|
|
public override bool Equals(object? obj) => obj is Ranking otherEvent && Equals(otherEvent);
|
|
|
|
public bool Equals(Ranking? other)
|
|
{
|
|
if (other is null) return false;
|
|
if (ReferenceEquals(this, other)) return true;
|
|
return
|
|
Id == other.Id
|
|
|| GetHashCode() == other.GetHashCode()
|
|
|| (TotalVotes == other.TotalVotes && UpVotes == other.UpVotes && DownVotes == other.DownVotes);
|
|
}
|
|
|
|
public static bool operator ==(Ranking ranking1, Ranking ranking2)
|
|
{
|
|
if (ranking1 is null && ranking2 is null) return true;
|
|
if (ranking1 is null || ranking2 is null) return false;
|
|
return ranking1.Equals(ranking2);
|
|
}
|
|
|
|
public static bool operator !=(Ranking ranking1, Ranking ranking2)
|
|
{
|
|
if (ranking1 is null && ranking2 is null) return false;
|
|
if (ranking1 is null || ranking2 is null) return true;
|
|
return !ranking1.Equals(ranking2);
|
|
}
|
|
|
|
public static bool operator < (Ranking ranking1, Ranking ranking2)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(ranking1, nameof(ranking1));
|
|
ArgumentNullException.ThrowIfNull(ranking2, nameof(ranking2));
|
|
return ranking1.Score < ranking2.Score;
|
|
}
|
|
|
|
public static bool operator > (Ranking ranking1, Ranking ranking2)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(ranking1, nameof(ranking1));
|
|
ArgumentNullException.ThrowIfNull(ranking2, nameof(ranking2));
|
|
if (ranking1 is null && ranking2 is null) return true;
|
|
if (ranking1 is null || ranking2 is null) return false;
|
|
return ranking1.Score > ranking2.Score;
|
|
}
|
|
|
|
public static bool operator <= (Ranking ranking1, Ranking ranking2)
|
|
=> ranking1 == ranking2 || ranking1 < ranking2;
|
|
|
|
public static bool operator >= (Ranking ranking1, Ranking ranking2)
|
|
=> ranking1 == ranking2 || ranking1 > ranking2;
|
|
}
|