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 NoteList { get; set; } [DataMember] public List TagList { get; private set; } [DataMember] public List FavList { get; private set; } [DataMember(EmitDefaultValue = false)] public bool IsConnected { get; set; } [DataMember] public Dictionary> NoteTagged { get; set; } public List 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(); TagList = new List(); FavList = new List(); NoteTagged = new Dictionary>(); AddedTheme = new List(); } public override string ToString() => $"username: {Username}\nemail: {Email}\npassword: {Password}\nOwned notes: {NoteList.Count}"; /// /// rechercher une note dans la liste de note de l'utilisateur et la liste de note favoris de l'utilisateur /// public List SearchNoteByName(List toResearchIntoList, string name) { List searchedNotes = new List(); string search = name.ToLower(); foreach (Note note in toResearchIntoList) { if (note.Name.ToLower().Contains(search)) { searchedNotes.Add(note); } } return searchedNotes; } public List SearchNoteByTag(List toResearchIntoList, string name) { List searchedNotes = new(); Tags tagtoresearchby = GetTagByName(name); foreach(Note note in toResearchIntoList) { if (NoteTagged[note].Contains(tagtoresearchby)) { searchedNotes.Add(note); } } return searchedNotes; } /// /// rechercher un tag dans la liste de tag de l'utilisateur /// public List SearchTagByName(List ToResearchIntoList, string name) { List searchedTags = new List(); string search = name.ToLower(); foreach (Tags tag in ToResearchIntoList) { if (tag.Name.ToLower().Contains(search)) { searchedTags.Add(tag); } } return searchedTags; } /// /// rechercher par date de création ou de modification /// /// /// /// /// /// public List SearchNoteByDate(List ToResearchIntoList, string name, DateOnly FirstDate, DateOnly SecondeDate) { List searchedNotes = new List(); 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; } /// /// rechercher par nom de la note /// /// /// /// public Note GetNoteByName(string name) { foreach (Note note in NoteList) { if (note.Name == name) { return note; } } throw new NotFoundException("Note not found"); } /// /// rechercher par nom du tag /// /// /// /// public Tags GetTagByName(string name) { foreach (Tags tag in TagList) { if (tag.Name == name) { return tag; } } throw new NotFoundException("Tag not found"); } /// /// ajouter une note dans la liste de note favorite de l'utilisateur /// public void AddFavorite(Note note) { if (FavList.Contains(note)) { throw new AlreadyExistException("Note already in favorites"); } FavList.Add(note); } /// /// supprimer une note dans la liste de note favorite de l'utilisateur /// public void RemoveFavorite(Note note) { if (FavList.Contains(note)) { FavList.Remove(note); } else { throw new NotFoundException("Note not found"); } } /// ///creer une note /// 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()); return note; } /// /// supprimer une note /// 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"); } /// /// creer un tag /// 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)); } /// /// supprimer un tag /// 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; } /// /// ajouter un tag a une note /// 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); } /// /// supprimer un tag a une note /// 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); } /// /// ajouter plusieur tag a une note /// public void AddTagsToNoteList(Note note, List tags) { NoteTagged.Add(note, tags); } /// ///supprimer tout les tags d'une note /// public void RemoveTagsFromNoteList(Note note) { NoteTagged.Remove(note); } /// /// modifier le nom d'une note /// /// /// /// 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; } /// /// modifier le mot de passe de l'utilisateur /// /// /// 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(); } /// /// modifier le theme de l'utilisateur /// /// /// 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 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]); } } /// /// ajouter un theme dans la liste de theme /// public void AddTheme(Theme theme) { foreach (Theme existingTheme in AddedTheme) { if (existingTheme.Name == theme.Name) { throw new AlreadyUsedException("Theme already used."); } } AddedTheme.Add(theme); } /// /// supprimer un theme dans la liste de theme /// 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."); } } /// /// recuperer un theme dans la liste de theme ajouté par l'utilisateur /// /// /// /// public Theme GetTheme(string name) { foreach (Theme theme in AddedTheme) { if (theme.Name == name) { return theme; } } throw new NotFoundException("Theme not found."); } /// /// modifier l'email de l'utilisateur /// /// /// public void ChangeEmail(string newEmail) { if (newEmail == null) { return; } if (Email == newEmail) { throw new AlreadyUsedException("this email is the same."); } Email = newEmail; } /// /// changer la photo de profil de l'utilisateur /// /// public void ChangeProfilePicture(string path) { List 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; } } }