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,394 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using District_3_App.Enitities;
namespace District_3_App.Repository
{
public class FancierProfile
{
public Guid ProfileId { get; set; }
public List<string> Links { get; set; }
public string DailyMotto { get; set; }
public DateTime? RemoveMottoDate { get; set; }
public int FrameNumber { get; set; }
public string Hashtag { get; set; }
}
public class FancierProfileRepo
{
private Dictionary<Guid, FancierProfile> profileRepo = new Dictionary<Guid, FancierProfile>();
private string filePath;
public FancierProfileRepo()
{
filePath = GenerateDefaultFilePath();
Console.WriteLine(filePath);
if (!File.Exists(filePath))
{
CreateXml(filePath);
}
Load(filePath);
}
private string GenerateDefaultFilePath()
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FancierProfile.xml");
}
private void CreateXml(string filePath)
{
XDocument xDocument = new XDocument(new XElement("FancierProfiles"));
xDocument.Save(filePath);
}
private void Load(string filePath)
{
Console.WriteLine("Reading profile info fancy settings from file: " + filePath);
XDocument xDocument = XDocument.Load(filePath);
XElement root = xDocument.Element("FancierProfiles");
if (root != null && root.HasElements)
{
foreach (var userElem in root.Elements("FancierProfile"))
{
Guid userId;
if (!Guid.TryParse((string)userElem.Attribute("ProfileId"), out userId))
{
userId = Guid.NewGuid();
}
FancierProfile profile = new FancierProfile();
try
{
profile.ProfileId = userId;
profile.DailyMotto = (string)userElem.Attribute("DailyMotto");
profile.RemoveMottoDate = (DateTime)userElem.Attribute("RemoveMottoDate");
if (profile.RemoveMottoDate < DateTime.Now)
{
profile.DailyMotto = null;
profile.RemoveMottoDate = null;
}
var linksElem = userElem.Element("Links");
if (linksElem != null)
{
profile.Links = linksElem.Elements("Link")
.Select(e => e.Value)
.Where(e => !string.IsNullOrEmpty(e))
.ToList();
}
profile.FrameNumber = (int)userElem.Attribute("FrameNumber");
profile.Hashtag = (string)userElem.Attribute("Hashtag");
profileRepo.Add(userId, profile);
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing profile: {ex.Message}");
}
}
}
}
public void SaveToXml()
{
try
{
XDocument xDocument;
if (File.Exists(filePath))
{
xDocument = XDocument.Load(filePath);
}
else
{
xDocument = new XDocument(new XElement("FancierProfiles"));
}
XElement root = xDocument.Element("FancierProfiles");
root?.RemoveAll();
foreach (var profileId in profileRepo.Keys)
{
FancierProfile profile = profileRepo[profileId];
XElement profileElement = new XElement("FancierProfile",
new XAttribute("ProfileId", profile.ProfileId),
new XAttribute("DailyMotto", profile.DailyMotto),
new XAttribute("RemoveMottoDate", profile.RemoveMottoDate),
new XAttribute("FrameNumber", profile.FrameNumber),
new XAttribute("Hashtag", profile.Hashtag));
if (profile.Links != null && profile.Links.Any())
{
XElement linksElement = new XElement("Links");
foreach (var link in profile.Links)
{
linksElement.Add(new XElement("Link", link));
}
profileElement.Add(linksElement);
}
root.Add(profileElement);
}
xDocument.Save(filePath);
}
catch (Exception ex)
{
Console.WriteLine("Error saving profiles to XML: " + ex.Message);
}
}
public bool AddLink(Guid userId, string newLink)
{
try
{
if (profileRepo.ContainsKey(userId))
{
if (profileRepo[userId].Links == null)
{
profileRepo[userId].Links = new List<string>();
}
profileRepo[userId].Links.Add(newLink);
SaveToXml();
return true;
}
else
{
FancierProfile profile = new FancierProfile
{
ProfileId = userId,
Links = new List<string> { newLink }
};
profileRepo.Add(userId, profile);
SaveToXml();
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Error adding link: " + ex.Message);
return false;
}
}
public bool DeleteLink(Guid userId, string linkToDelete)
{
try
{
if (profileRepo.ContainsKey(userId))
{
if (profileRepo[userId].Links != null)
{
profileRepo[userId].Links.Remove(linkToDelete);
SaveToXml();
}
return true;
}
else
{
Console.WriteLine("User ID not found.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine("Error deleting link: " + ex.Message);
return false;
}
}
public bool SetFrameNumber(Guid userId, int newFrameNumber)
{
try
{
if (profileRepo.ContainsKey(userId))
{
profileRepo[userId].FrameNumber = newFrameNumber;
SaveToXml();
return true;
}
else
{
FancierProfile profile = new FancierProfile
{
ProfileId = userId,
FrameNumber = newFrameNumber,
};
profileRepo.Add(userId, profile);
SaveToXml();
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Error setting frame number: " + ex.Message);
return false;
}
}
public bool DeleteFrameNumber(Guid userId)
{
try
{
if (profileRepo.ContainsKey(userId))
{
profileRepo[userId].FrameNumber = 0;
SaveToXml();
return true;
}
else
{
Console.WriteLine("User ID not found.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine("Error deleting frame number: " + ex.Message);
return false;
}
}
public bool SetHashtag(Guid userId, string newHashtag)
{
try
{
if (profileRepo.ContainsKey(userId))
{
profileRepo[userId].Hashtag = newHashtag;
SaveToXml();
return true;
}
else
{
FancierProfile profile = new FancierProfile
{
ProfileId = userId,
Hashtag = newHashtag,
};
profileRepo.Add(userId, profile);
SaveToXml();
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Error setting hashtag: " + ex.Message);
return false;
}
}
public bool DeleteHashtag(Guid userId)
{
try
{
if (profileRepo.ContainsKey(userId))
{
profileRepo[userId].Hashtag = string.Empty;
SaveToXml();
return true;
}
else
{
Console.WriteLine("User ID not found.");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine("Error deleting hashtag: " + ex.Message);
return false;
}
}
public bool AddDailyMotto(Guid userId, string newMotto, DateTime dateToRemove)
{
if (profileRepo.ContainsKey(userId))
{
profileRepo[userId].DailyMotto = newMotto;
profileRepo[userId].RemoveMottoDate = dateToRemove;
SaveToXml();
return true;
}
else
{
FancierProfile profile = new FancierProfile();
profile.DailyMotto = newMotto;
profile.RemoveMottoDate = dateToRemove;
profileRepo.Add(userId, profile);
return true;
}
return false;
}
public bool DeleteDailyMotto(Guid userId)
{
if (profileRepo.ContainsKey(userId))
{
profileRepo[userId].DailyMotto = null;
SaveToXml();
return true;
}
else
{
return false;
}
}
public string GetDailyMotto(Guid userId)
{
if (profileRepo.ContainsKey(userId))
{
return profileRepo[userId].DailyMotto;
}
else
{
return null;
}
}
public List<string> GetLinks(Guid userId)
{
if (profileRepo.ContainsKey(userId))
{
return profileRepo[userId].Links;
}
else
{
return null;
}
}
public int GetFrameNumber(Guid userId)
{
if (profileRepo.ContainsKey(userId))
{
return profileRepo[userId].FrameNumber;
}
else
{
return -1;
}
}
public string GetHashtag(Guid userId)
{
if (profileRepo.ContainsKey(userId))
{
return profileRepo[userId].Hashtag;
}
else
{
return null;
}
}
}
}
@@ -0,0 +1,279 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using District_3_App.Enitities;
using District_3_App.Enitities.Mocks;
namespace District_3_App.Repository
{
public class HighlightsRepo
{
private Dictionary<Guid, Dictionary<Guid, Highlight>> userHighlights = new Dictionary<Guid, Dictionary<Guid, Highlight>>();
private string filePath;
public HighlightsRepo()
{
filePath = GenerateDefaultFilePath();
Console.WriteLine(filePath);
if (!File.Exists(filePath))
{
CreateXml(filePath);
}
LoadHighlights(filePath);
}
private string GenerateDefaultFilePath()
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Highlights.xml");
}
private void CreateXml(string filePath)
{
XDocument xDocument = new XDocument(new XElement("Highlights"));
xDocument.Save(filePath);
}
private void LoadHighlights(string filePath)
{
Console.WriteLine("Reading highlights from file: " + filePath);
XDocument xDocument = XDocument.Load(filePath);
XElement root = xDocument.Element("Highlights");
if (root != null && root.HasElements)
{
foreach (var userElem in root.Elements("User"))
{
Guid userId;
if (!Guid.TryParse((string)userElem.Attribute("userId"), out userId))
{
// Skip this user element if userId attribute is not valid
continue;
}
foreach (var highlightElem in userElem.Elements("Highlight"))
{
Highlight highlight = new Highlight();
highlight.SetUserId(userId);
// Wrap the parsing in a try-catch block
try
{
Guid guid;
if (Guid.TryParse((string)highlightElem.Attribute("guid"), out guid))
{
highlight.SetGuid(guid);
}
highlight.SetName((string)highlightElem.Attribute("name"));
var postsElem = highlightElem.Element("posts");
if (postsElem != null)
{
// Parse post elements
var posts = postsElem.Elements("post").Select(e =>
{
Guid postGuid;
if (Guid.TryParse(e.Value, out postGuid))
{
return postGuid;
}
else
{
return Guid.Empty; // or any other default value
}
}).Where(e => e != Guid.Empty).ToList();
highlight.SetListPosts(posts);
}
highlight.SetCover((string)highlightElem.Attribute("cover"));
if (!userHighlights.ContainsKey(userId))
{
userHighlights[userId] = new Dictionary<Guid, Highlight>();
}
userHighlights[userId].Add(highlight.GetHighlightId(), highlight);
}
catch (Exception ex)
{
// Log the error or handle it as needed
Console.WriteLine($"Error parsing highlight: {ex.Message}");
}
}
}
}
}
public void SaveHighlightsToXml()
{
try
{
XDocument xDocument;
if (File.Exists(filePath))
{
xDocument = XDocument.Load(filePath);
}
else
{
xDocument = new XDocument(new XElement("Highlights"));
}
XElement root = xDocument.Element("Highlights");
root?.RemoveAll();
foreach (var userHighlight in userHighlights)
{
XElement userElement = new XElement("User", new XAttribute("userId", userHighlight.Key));
foreach (var highlight in userHighlight.Value)
{
Highlight highlight1 = highlight.Value;
XElement highlightElement = new XElement("Highlight",
new XAttribute("userId", userHighlight.Key),
new XAttribute("guid", highlight1.GetHighlightId()), // Use the highlight's guid
new XAttribute("name", highlight1.GetName()), // Use the highlight's name
new XElement("posts", highlight1.GetPosts().Select(p => new XElement("post", p))),
new XAttribute("cover", highlight1.GetCover()));
userElement.Add(highlightElement);
}
root.Add(userElement);
}
xDocument.Save(filePath);
}
catch (Exception ex)
{
Console.WriteLine("Error saving highlights to XML: " + ex.Message);
}
}
public List<MockPhotoPost> GetConnectedUserPosts(Guid userId)
{
List<MockPhotoPost> posts = new List<MockPhotoPost>();
string path1 = "/Images/snow.jpg";
string path2 = "/Images/peeta.jpeg";
string path3 = "/Images/katniss.jpg";
string path4 = "/Images/poster.jpeg";
MockPhotoPost post1 = new MockPhotoPost(userId, new Dictionary<int, List<object>>(), new List<object>(), "Title 1", "Description 1", path1);
post1.SetPostId(new Guid("11111111-1111-1111-1111-111111111111"));
MockPhotoPost post2 = new MockPhotoPost(userId, new Dictionary<int, List<object>>(), new List<object>(), "Title 2", "Description 2", path2);
post2.SetPostId(new Guid("22222222-2222-2222-2222-222222222222"));
MockPhotoPost post3 = new MockPhotoPost(userId, new Dictionary<int, List<object>>(), new List<object>(), "Title 3", "Description 3", path3);
post3.SetPostId(new Guid("33333333-3333-3333-3333-333333333333"));
MockPhotoPost post4 = new MockPhotoPost(userId, new Dictionary<int, List<object>>(), new List<object>(), "Title 4", "Description 4", path4);
post4.SetPostId(new Guid("44444444-4444-4444-4444-444444444444"));
posts.Add(post1);
posts.Add(post2);
posts.Add(post3);
posts.Add(post4);
return posts;
}
public bool AddHighlight(Guid userId, Highlight highlight)
{
if (!userHighlights.ContainsKey(userId))
{
userHighlights[userId] = new Dictionary<Guid, Highlight>();
}
userHighlights[userId].Add(highlight.GetHighlightId(), highlight);
SaveHighlightsToXml();
return true;
}
public bool RemoveHighlight(Guid userId, Guid highlightId)
{
if (userHighlights.ContainsKey(userId) && userHighlights[userId].ContainsKey(highlightId))
{
userHighlights[userId].Remove(highlightId);
SaveHighlightsToXml();
return true;
}
return false;
}
public bool AddPostToHighlight(Guid userId, Guid postId, Guid highlightId)
{
if (userHighlights.ContainsKey(userId) && userHighlights[userId].ContainsKey(highlightId))
{
List<Guid> listPosts = userHighlights[userId][highlightId].GetPosts();
if (!listPosts.Contains(postId))
{
listPosts.Add(postId);
userHighlights[userId][highlightId].SetListPosts(listPosts);
SaveHighlightsToXml();
return true;
}
}
return false;
}
public bool RemovePostFromHighlight(Guid userId, Guid postId, Guid highlightId)
{
if (userHighlights.ContainsKey(userId) && userHighlights[userId].ContainsKey(highlightId))
{
List<Guid> listPosts = userHighlights[userId][highlightId].GetPosts();
if (listPosts.Contains(postId))
{
listPosts.Remove(postId);
userHighlights[userId][highlightId].SetListPosts(listPosts);
SaveHighlightsToXml();
return true;
}
}
return false;
}
private List<Highlight> GetHighlights()
{
return userHighlights.Values.SelectMany(dict => dict.Values).ToList();
}
public List<Highlight> GetHighlightsOfUser(Guid userId)
{
if (userHighlights.ContainsKey(userId))
{
return userHighlights[userId].Values.ToList();
}
else
{
return new List<Highlight>();
}
}
public Highlight GetHighlight(Guid userId, Guid highlightId)
{
if (userHighlights.ContainsKey(userId) && userHighlights[userId].ContainsKey(highlightId))
{
return userHighlights[userId][highlightId];
}
return null;
}
public List<MockPhotoPost> GetPostsOfHighlight(Guid userId, Guid highlightId)
{
if (userHighlights.ContainsKey(userId) && userHighlights[userId].ContainsKey(highlightId))
{
List<MockPhotoPost> postsOfHighlight = new List<MockPhotoPost>();
Highlight highlight = userHighlights[userId][highlightId];
foreach (var post in GetConnectedUserPosts(userId))
{
if (highlight.GetPosts().Contains(post.GetPostId()))
{
postsOfHighlight.Add(post);
}
}
return postsOfHighlight;
}
return new List<MockPhotoPost>();
}
}
}
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using District_3_App.Enitities;
using District_3_App.Enitities.Mocks;
namespace District_3_App.Repository
{
public class SnapshotsRepo
{
private HighlightsRepo highlightsRepo = new HighlightsRepo();
private Guid userId;
public SnapshotsRepo(Guid userId)
{
this.userId = userId;
}
public bool AddHighlight(Highlight highlight)
{
return highlightsRepo.AddHighlight(userId, highlight);
}
public bool RemoveHighlight(Highlight highlight)
{
return highlightsRepo.RemoveHighlight(userId, highlight.GetHighlightId());
}
public bool AddPostToHighlight(Guid highlightId, Guid postId)
{
return highlightsRepo.AddPostToHighlight(userId, highlightId, postId);
}
public bool RemovePostFromHighlight(Guid highlightId, Guid postId)
{
return highlightsRepo.RemovePostFromHighlight(userId, highlightId, postId);
}
public HighlightsRepo GetHighlightsRepo()
{
return highlightsRepo;
}
public List<Highlight> GetHighlightsOfUser()
{
return highlightsRepo.GetHighlightsOfUser(userId);
}
public Highlight GetHighlight(Guid highlightId)
{
return highlightsRepo.GetHighlight(userId, highlightId);
}
public List<MockPhotoPost> GetPostsOfHighlight(Guid highlightId)
{
return highlightsRepo.GetPostsOfHighlight(userId, highlightId);
}
}
}