You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SAE-2.01/MCTG/Model/Recipes/Review.cs

100 lines
2.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Model
{
[DataContract(Name = "review")]
public class Review : IEquatable<Review>
{
[DataMember(Name = "stars")]
private int _stars;
[DataMember(Name = "content")]
private string _content = "";
[DataMember(Name = "id")]
public int Id { get; init; }
[DataMember(Name = "user")]
public User Author { get; private set; }
public int Stars
{
get => _stars;
set
{
if (value < 0 || value > 5) throw new ArgumentException(nameof(Stars));
else _stars = value;
}
}
public string Content
{
get => _content;
set
{
if (string.IsNullOrEmpty(value)) _content = "No data...";
else _content = value;
}
}
public Review(User author, int? id, int stars, string content)
{
if (id == null)
{
var randomGenerator = RandomNumberGenerator.Create();
byte[] data = new byte[16];
randomGenerator.GetBytes(data);
Id = Math.Abs(BitConverter.ToInt16(data));
}
else Id = (int)id;
Author = author;
Stars = stars;
Content = content;
}
public Review(User author, int stars, string content)
: this(author, null, stars, content)
{
}
public Review(int stars, string content)
: this(new User("admin", "admin", "admin@mctg.fr", new PasswordManager().HashPassword("admin")),
null, stars, content)
{
}
public override string ToString()
{
return $"{Author.Name} {Author.Surname}: [ {Stars} stars ]\n{Content}";
}
public bool Equals(Review? other)
{
if (other is null) return false;
return Id.Equals(other.Id);
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(obj, null)) return false;
if (ReferenceEquals(obj, this)) return true;
if (GetType() != obj.GetType()) return false;
return Equals(obj);
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
}