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.
notus/notus/Biblioteque_de_Class/User.cs

499 lines
16 KiB

using Microsoft.VisualBasic.FileIO;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.Serialization;
namespace Biblioteque_de_Class
{
[DataContract(IsReference = true)]
public class User
{
[DataMember]
public string Username { get; set; }
[DataMember]
public string Email { get; private set; }
[DataMember]
public string Password { get; private set; }
[DataMember]
public string Picture { get; private set; }
[DataMember]
public Theme UseTheme { get; set; }
[DataMember]
public List<Note> NoteList { get; set; }
[DataMember]
public List<Tags> TagList { get; private set; }
[DataMember]
public List<Note> FavList { get; private set; }
[DataMember(EmitDefaultValue = false)]
public bool IsConnected { get; set; }
[DataMember]
public Dictionary<Note, List<Tags>> NoteTagged { get; set; }
public List<Theme> AddedTheme { get; set; }
public User(string username, string email, string password)
{
Username = username;
Email = email;
Password = password;
Picture = "defaultpicture.png";
UseTheme = new("", ",,".Split().ToList());
NoteList = new List<Note>();
TagList = new List<Tags>();
FavList = new List<Note>();
NoteTagged = new Dictionary<Note, List<Tags>>();
AddedTheme = new List<Theme>();
}
public override string ToString() => $"username: {Username}\nemail: {Email}\npassword: {Password}\nOwned notes: {NoteList.Count}";
/// <summary>
/// rechercher une note dans la liste de note de l'utilisateur et la liste de note favoris de l'utilisateur
/// </summary>
public List<Note> SearchNoteByName(List<Note> toResearchIntoList, string name)
{
List<Note> searchedNotes = new List<Note>();
string search = name.ToLower();
foreach (Note note in toResearchIntoList)
{
if (note.Name.ToLower().Contains(search))
{
searchedNotes.Add(note);
}
}
return searchedNotes;
}
public List<Note> SearchNoteByTag(List<Note> toResearchIntoList, string name)
{
List<Note> searchedNotes = new();
Tags tagtoresearchby = GetTagByName(name);
foreach(Note note in toResearchIntoList)
{
if (NoteTagged[note].Contains(tagtoresearchby))
{
searchedNotes.Add(note);
}
}
return searchedNotes;
}
/// <summary>
/// rechercher un tag dans la liste de tag de l'utilisateur
/// </summary>
public List<Tags> SearchTagByName(List<Tags> ToResearchIntoList, string name)
{
List<Tags> searchedTags = new List<Tags>();
string search = name.ToLower();
foreach (Tags tag in ToResearchIntoList)
{
if (tag.Name.ToLower().Contains(search))
{
searchedTags.Add(tag);
}
}
return searchedTags;
}
/// <summary>
/// rechercher par date de création ou de modification
/// </summary>
/// <param name="ToResearchIntoList"></param>
/// <param name="name"></param>
/// <param name="FirstDate"></param>
/// <param name="SecondeDate"></param>
/// <returns></returns>
public List<Note> SearchNoteByDate(List<Note> ToResearchIntoList, string name, DateOnly FirstDate, DateOnly SecondeDate)
{
List<Note> searchedNotes = new List<Note>();
string search = name.ToLower();
foreach (Note note in ToResearchIntoList)
{
if (note.Name.ToLower().Contains(search) && note.CreationDate >= FirstDate && note.CreationDate <= SecondeDate || note.ModificationDate >= FirstDate && note.ModificationDate <= SecondeDate)
{
searchedNotes.Add(note);
}
}
return searchedNotes;
}
/// <summary>
/// rechercher par nom de la note
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
/// <exception cref="NotFoundException"></exception>
public Note GetNoteByName(string name)
{
foreach (Note note in NoteList)
{
if (note.Name == name)
{
return note;
}
}
throw new NotFoundException("Note not found");
}
/// <summary>
/// rechercher par nom du tag
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
/// <exception cref="NotFoundException"></exception>
public Tags GetTagByName(string name)
{
foreach (Tags tag in TagList)
{
if (tag.Name == name)
{
return tag;
}
}
throw new NotFoundException("Tag not found");
}
/// <summary>
/// ajouter une note dans la liste de note favorite de l'utilisateur
/// </summary>
public void AddFavorite(Note note)
{
if (FavList.Contains(note))
{
throw new AlreadyExistException("Note already in favorites");
}
FavList.Add(note);
}
/// <summary>
/// supprimer une note dans la liste de note favorite de l'utilisateur
/// </summary>
public void RemoveFavorite(Note note)
{
if (FavList.Contains(note))
{
FavList.Remove(note);
}
else
{
throw new NotFoundException("Note not found");
}
}
/// <summary>
///creer une note
/// </summary>
public Note CreateNote(string name, string imagePath)
{
foreach (Note existingNote in NoteList)
{
if (existingNote.Name == name)
{
throw new AlreadyExistException("Note already exists");
}
}
Note tocheckid = NoteList.Last();
Note note = new Note(tocheckid.id + 1, name, imagePath, this);
NoteList.Add(note);
NoteTagged.Add(note, new List<Tags>());
return note;
}
/// <summary>
/// supprimer une note
/// </summary>
public void DeleteNote(Note note)
{
note.VerifyOwner(this);
foreach (Note existingNote in NoteList)
{
if (existingNote == note)
{
NoteList.Remove(existingNote);
NoteTagged.Remove(existingNote);
return;
}
}
throw new NotFoundException("Note not found");
}
/// <summary>
/// creer un tag
/// </summary>
public void CreateTag(string name, string color)
{
foreach (Tags tag in TagList)
{
if (tag.Name == name)
{
throw new AlreadyExistException("Tag already exists");
}
}
TagList.Add(new Tags(name, color));
}
/// <summary>
/// supprimer un tag
/// </summary>
public void DeleteTag(Tags tagtodelete)
{
foreach (Tags tag in TagList)
{
if (tag == tagtodelete)
{
TagList.Remove(tag);
return;
}
}
throw new NotFoundException("Tag not found");
}
public void EditTagName(Tags tag, string newName)
{
if (!TagList.Contains(tag))
{
throw new NotFoundException("Tag not found");
}
else
{
foreach (Tags existingTag in TagList)
{
if (existingTag.Name == newName)
{
throw new AlreadyExistException("Tag already exists");
}
}
}
tag.Name = newName;
}
public void EditTagColor(Tags tag, string newColor)
{
if (!TagList.Contains(tag))
{
throw new NotFoundException("Tag not found");
}
tag.Color = newColor;
}
/// <summary>
/// ajouter un tag a une note
/// </summary>
public void AddTagFromNoteList(Note note, Tags tagToAdd)
{
if (!TagList.Contains(tagToAdd))
{
throw new NotFoundException("Tag not found");
}
if (!NoteList.Contains(note))
{
throw new NotFoundException("Note not found");
}
NoteTagged[note].Add(tagToAdd);
}
/// <summary>
/// supprimer un tag a une note
/// </summary>
public void RemoveTagFromNoteList(Note note, Tags tagToRemove)
{
if (!TagList.Contains(tagToRemove))
{
throw new NotFoundException("Tag not found");
}
if (!NoteList.Contains(note))
{
throw new NotFoundException("Note not found");
}
NoteTagged[note].Remove(tagToRemove);
}
/// <summary>
/// ajouter plusieur tag a une note
/// </summary>
public void AddTagsToNoteList(Note note, List<Tags> tags)
{
NoteTagged.Add(note, tags);
}
/// <summary>
///supprimer tout les tags d'une note
/// </summary>
public void RemoveTagsFromNoteList(Note note)
{
NoteTagged.Remove(note);
}
/// <summary>
/// modifier le nom d'une note
/// </summary>
/// <param name="note"></param>
/// <param name="newname"></param>
/// <exception cref="AlreadyUsedException"></exception>
public void SetNoteName(Note note, string newname)
{
foreach(Note n in NoteList)
{
if(n.Name == note.Name)
{
throw new AlreadyUsedException("This name is already used");
}
}
note.Name = newname;
}
/// <summary>
/// modifier le mot de passe de l'utilisateur
/// </summary>
/// <param name="newPassword"></param>
/// <exception cref="AlreadyExistException"></exception>
public void ChangePassword(string newPassword)
{
if (newPassword == null) { return; }
if ( Password == newPassword) { throw new AlreadyExistException("this username is the same."); }
if (newPassword.Length < 8) { throw new NotAllowedException("this password is too short."); }
Password = HashCodeModel.GetSHA256Hash(newPassword).ToString();
}
/// <summary>
/// modifier le theme de l'utilisateur
/// </summary>
/// <param name="theme"></param>
/// <exception cref="AlreadyExistException"></exception>
public void ChangeTheme(Theme theme)
{
if (UseTheme.Name == theme.Name)
{
throw new AlreadyExistException("this theme is already selected.");
}
UseTheme = theme;
}
public void ChangeThemeName(Theme theme, string newName)
{
foreach (Theme existingTheme in AddedTheme)
{
if (existingTheme.Name == newName)
{
throw new AlreadyExistException("this theme is already selected.");
}
}
if (theme.Name == newName)
{
throw new AlreadyExistException("this theme is already selected.");
}
theme.Name = newName;
}
public void ChangeThemeColors(Theme theme, List<string> newColor)
{
if(theme.ColorList == newColor)
{
throw new AlreadyExistException("this theme those colors");
}
if(theme.ColorList.Count != newColor.Count)
{
throw new NotFoundException("this theme doesn't have the same number of colors");
}
for (int i = 0; i < theme.ColorList.Count; i++)
{
theme.ChangeColor(theme.ColorList[i], newColor[i]);
}
}
/// <summary>
/// ajouter un theme dans la liste de theme
/// </summary>
public void AddTheme(Theme theme)
{
foreach (Theme existingTheme in AddedTheme)
{
if (existingTheme.Name == theme.Name)
{
throw new AlreadyUsedException("Theme already used.");
}
}
AddedTheme.Add(theme);
}
/// <summary>
/// supprimer un theme dans la liste de theme
/// </summary>
public void RemoveTheme(Theme theme)
{
if (AddedTheme.Contains(theme))
{
if (theme.Name.Length > 6 && theme.Name[..6] != "Static")
{
throw new AlreadyUsedException("This theme is used a default theme.");
}
AddedTheme.Remove(theme);
}
else
{
throw new NotFoundException("Theme not found.");
}
}
/// <summary>
/// recuperer un theme dans la liste de theme ajouté par l'utilisateur
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
/// <exception cref="NotFoundException"></exception>
public Theme GetTheme(string name)
{
foreach (Theme theme in AddedTheme)
{
if (theme.Name == name)
{
return theme;
}
}
throw new NotFoundException("Theme not found.");
}
/// <summary>
/// modifier l'email de l'utilisateur
/// </summary>
/// <param name="newEmail"></param>
/// <exception cref="AlreadyExistException"></exception>
public void ChangeEmail(string newEmail)
{
if (newEmail == null) { return; }
if (Email == newEmail) { throw new AlreadyUsedException("this email is the same."); }
Email = newEmail;
}
/// <summary>
/// changer la photo de profil de l'utilisateur
/// </summary>
/// <param name="path"></param>
public void ChangeProfilePicture(string path)
{
List<string> picture = new();
picture = path.Split('.').ToList();
string extension = picture.Last();
if (extension != "png" && extension != "jpg" && extension != "jpeg")
{
throw new NotFoundException("this extension is not allowed.");
}
if(File.Exists(path))
{
Picture = path;
}
else
{
throw new NotFoundException("this file doesn't exist.");
}
if( path == null)
{
Picture = "default.png";
}
}
public void SetDefaultTheme(Theme theme)
{
UseTheme = theme;
}
}
}