Skip to content

Commit

Permalink
[Backend/Models] Simplify clone functions (#2906)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Feb 14, 2024
1 parent 71b75cc commit 8677250
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 204 deletions.
11 changes: 3 additions & 8 deletions Backend/Models/MergeWordSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ public MergeWordSet()
Id = "";
ProjectId = "";
UserId = "";
WordIds = new List<string>();
WordIds = new();
}

public MergeWordSet Clone()
{
var clone = new MergeWordSet
return new()
{
Id = Id,
ProjectId = ProjectId,
UserId = UserId,
WordIds = new List<string>()
WordIds = WordIds.Select(id => id).ToList()
};
foreach (var id in WordIds)
{
clone.WordIds.Add(id);
}
return clone;
}

public bool ContentEquals(MergeWordSet other)
Expand Down
26 changes: 7 additions & 19 deletions Backend/Models/MergeWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class MergeWords

public MergeWords()
{
Parent = new Word();
Children = new List<MergeSourceWord>();
Parent = new();
Children = new();
DeleteOnly = false;
}
}
Expand Down Expand Up @@ -59,8 +59,8 @@ public class MergeUndoIds

public MergeUndoIds()
{
ParentIds = new List<string>();
ChildIds = new List<string>();
ParentIds = new();
ChildIds = new();
}

public MergeUndoIds(List<string> parentIds, List<string> childIds)
Expand All @@ -71,23 +71,11 @@ public MergeUndoIds(List<string> parentIds, List<string> childIds)

public MergeUndoIds Clone()
{
var clone = new MergeUndoIds
return new()
{
ParentIds = new List<string>(),
ChildIds = new List<string>()
ParentIds = ParentIds.Select(id => id).ToList(),
ChildIds = ChildIds.Select(id => id).ToList()
};

foreach (var id in ParentIds)
{
clone.ParentIds.Add(id);
}

foreach (var id in ChildIds)
{
clone.ChildIds.Add(id);
}

return clone;
}

public bool ContentEquals(MergeUndoIds other)
Expand Down
85 changes: 23 additions & 62 deletions Backend/Models/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@ public Project()
DefinitionsEnabled = false;
GrammaticalInfoEnabled = false;
AutocompleteSetting = AutocompleteSetting.On;
SemDomWritingSystem = new WritingSystem();
VernacularWritingSystem = new WritingSystem();
AnalysisWritingSystems = new List<WritingSystem>();
SemanticDomains = new List<SemanticDomain>();
ValidCharacters = new List<string>();
RejectedCharacters = new List<string>();
CustomFields = new List<CustomField>();
WordFields = new List<string>();
PartsOfSpeech = new List<string>();
InviteTokens = new List<EmailInvite>();
WorkshopSchedule = new List<DateTime>();
SemDomWritingSystem = new();
VernacularWritingSystem = new();
AnalysisWritingSystems = new();
SemanticDomains = new();
ValidCharacters = new();
RejectedCharacters = new();
CustomFields = new();
WordFields = new();
PartsOfSpeech = new();
InviteTokens = new();
WorkshopSchedule = new();
}

public Project Clone()
{
var clone = new Project
return new()
{
Id = Id,
Name = Name,
Expand All @@ -118,55 +118,16 @@ public Project Clone()
AutocompleteSetting = AutocompleteSetting,
SemDomWritingSystem = SemDomWritingSystem.Clone(),
VernacularWritingSystem = VernacularWritingSystem.Clone(),
AnalysisWritingSystems = new List<WritingSystem>(),
SemanticDomains = new List<SemanticDomain>(),
ValidCharacters = new List<string>(),
RejectedCharacters = new List<string>(),
CustomFields = new List<CustomField>(),
WordFields = new List<string>(),
PartsOfSpeech = new List<string>(),
InviteTokens = new List<EmailInvite>(),
WorkshopSchedule = new List<DateTime>(),
AnalysisWritingSystems = AnalysisWritingSystems.Select(ws => ws.Clone()).ToList(),
SemanticDomains = SemanticDomains.Select(sd => sd.Clone()).ToList(),
ValidCharacters = ValidCharacters.Select(vc => vc).ToList(),
RejectedCharacters = RejectedCharacters.Select(rc => rc).ToList(),
CustomFields = CustomFields.Select(cf => cf.Clone()).ToList(),
WordFields = WordFields.Select(wf => wf).ToList(),
PartsOfSpeech = PartsOfSpeech.Select(ps => ps).ToList(),
InviteTokens = InviteTokens.Select(it => it.Clone()).ToList(),
WorkshopSchedule = WorkshopSchedule.Select(dt => dt).ToList(),
};

foreach (var aw in AnalysisWritingSystems)
{
clone.AnalysisWritingSystems.Add(aw.Clone());
}
foreach (var sd in SemanticDomains)
{
clone.SemanticDomains.Add(sd.Clone());
}
foreach (var cs in ValidCharacters)
{
clone.ValidCharacters.Add(cs);
}
foreach (var cs in RejectedCharacters)
{
clone.RejectedCharacters.Add(cs);
}
foreach (var cf in CustomFields)
{
clone.CustomFields.Add(cf.Clone());
}
foreach (var wf in WordFields)
{
clone.WordFields.Add(wf);
}
foreach (var pos in PartsOfSpeech)
{
clone.PartsOfSpeech.Add(pos);
}
foreach (var it in InviteTokens)
{
clone.InviteTokens.Add(it.Clone());
}
foreach (var dt in WorkshopSchedule)
{
clone.WorkshopSchedule.Add(dt);
}

return clone;
}

public bool ContentEquals(Project other)
Expand Down Expand Up @@ -362,8 +323,8 @@ public class UserCreatedProject

public UserCreatedProject()
{
Project = new Project();
User = new User();
Project = new();
User = new();
}
}

Expand Down
12 changes: 3 additions & 9 deletions Backend/Models/SemanticDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,15 @@ public SemanticDomainFull()
Name = "";
Id = "";
Description = "";
Questions = new List<string>();
Questions = new();
Lang = "";
}

public new SemanticDomainFull Clone()
{
var clone = (SemanticDomainFull)base.Clone();
clone.Description = Description;
clone.Questions = new List<string>();

foreach (var question in Questions)
{
clone.Questions.Add(question);
}

clone.Questions = Questions.Select(q => q).ToList();
return clone;
}

Expand Down Expand Up @@ -173,7 +167,7 @@ public SemanticDomainTreeNode(SemanticDomain sd)
Lang = sd.Lang;
Name = sd.Name;
Id = sd.Id;
Children = new List<SemanticDomain>();
Children = new();
}
}
}
39 changes: 10 additions & 29 deletions Backend/Models/Sense.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,25 @@ public Sense()
// By default generate a new, unique Guid for each new Sense.
Guid = Guid.NewGuid();
Accessibility = Status.Active;
GrammaticalInfo = new GrammaticalInfo();
Definitions = new List<Definition>();
Glosses = new List<Gloss>();
ProtectReasons = new List<ProtectReason>();
SemanticDomains = new List<SemanticDomain>();
GrammaticalInfo = new();
Definitions = new();
Glosses = new();
ProtectReasons = new();
SemanticDomains = new();
}

public Sense Clone()
{
var clone = new Sense
return new()
{
Guid = Guid,
Accessibility = Accessibility,
GrammaticalInfo = GrammaticalInfo.Clone(),
Definitions = new List<Definition>(),
Glosses = new List<Gloss>(),
ProtectReasons = new List<ProtectReason>(),
SemanticDomains = new List<SemanticDomain>(),
Definitions = Definitions.Select(d => d.Clone()).ToList(),
Glosses = Glosses.Select(g => g.Clone()).ToList(),
ProtectReasons = ProtectReasons.Select(pr => pr.Clone()).ToList(),
SemanticDomains = SemanticDomains.Select(sd => sd.Clone()).ToList(),
};

foreach (var definition in Definitions)
{
clone.Definitions.Add(definition.Clone());
}
foreach (var gloss in Glosses)
{
clone.Glosses.Add(gloss.Clone());
}
foreach (var reason in ProtectReasons)
{
clone.ProtectReasons.Add(reason.Clone());
}
foreach (var sd in SemanticDomains)
{
clone.SemanticDomains.Add(sd.Clone());
}

return clone;
}

public override bool Equals(object? obj)
Expand Down
8 changes: 4 additions & 4 deletions Backend/Models/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public SemanticDomainUserCount()
{
Id = "";
Username = "";
DomainSet = new HashSet<string>();
DomainSet = new();
DomainCount = 0;
WordCount = 0;
}
Expand Down Expand Up @@ -90,8 +90,8 @@ public class ChartRootData

public ChartRootData()
{
Dates = new List<string>();
Datasets = new List<Dataset>();
Dates = new();
Datasets = new();
}
}

Expand All @@ -107,7 +107,7 @@ public class Dataset
public Dataset(string userName, int data)
{
UserName = userName;
Data = new List<int>() { data };
Data = new() { data };
}
}

Expand Down
22 changes: 5 additions & 17 deletions Backend/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ public User()
UILang = "";
Token = "";
IsAdmin = false;
WorkedProjects = new Dictionary<string, string>();
ProjectRoles = new Dictionary<string, string>();
WorkedProjects = new();
ProjectRoles = new();
}

public User Clone()
{
var clone = new User
return new()
{
Id = Id,
Avatar = Avatar,
Expand All @@ -117,21 +117,9 @@ public User Clone()
UILang = UILang,
Token = Token,
IsAdmin = IsAdmin,
WorkedProjects = new Dictionary<string, string>(),
ProjectRoles = new Dictionary<string, string>()
WorkedProjects = WorkedProjects.ToDictionary(kv => kv.Key, kv => kv.Value),
ProjectRoles = ProjectRoles.ToDictionary(kv => kv.Key, kv => kv.Value),
};

foreach (var projId in WorkedProjects.Keys)
{
clone.WorkedProjects.Add(projId, WorkedProjects[projId]);
}

foreach (var projId in ProjectRoles.Keys)
{
clone.ProjectRoles.Add(projId, ProjectRoles[projId]);
}

return clone;
}

public bool ContentEquals(User other)
Expand Down
Loading

0 comments on commit 8677250

Please sign in to comment.