Skip to content

Commit

Permalink
Merge pull request #7 from TheKornelM/dev
Browse files Browse the repository at this point in the history
New translations, class data validators
  • Loading branch information
TheKornelM authored Nov 20, 2024
2 parents 47843f1 + a1574a9 commit 1044b7b
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 8 deletions.
34 changes: 27 additions & 7 deletions SchoolManagerModel/Persistence/SubjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,43 @@ public async Task AddSubjectAsync(Subject subject)
public async Task AssignSubjectsToStudentAsync(Student student, List<Subject> subjects)
{
var dbStudent = dbContext.Students
.Include(x => x.Subjects)
.FirstOrDefault(x => x.Id == student.Id);
.Where(x => x.Id == student.Id)
.Select(x => new
{
Student = x,
Subjects = x.Subjects ?? new List<AssignedSubject>()
})
.FirstOrDefault();

if (dbStudent == null)
throw new Exception("Student not found");

if (dbStudent == null) throw new Exception("Student not found");
dbStudent.Student.Subjects = dbStudent.Subjects;

dbStudent.Subjects = dbStudent.Subjects ?? [];
// Ensure all subjects are tracked by the DbContext
var dbSubjects = await dbContext.Subjects
.Where(s => subjects.Select(sub => sub.Id).Contains(s.Id))
.ToListAsync();

foreach (var dbSubject in dbSubjects)
{
if (dbStudent.Subjects.Any(subject => subject.Subject.Id == dbSubject.Id))
{
return;
}

foreach (var item in subjects)
dbStudent.Subjects.Add(new AssignedSubject
{
Subject = item,
Student = student
Subject = dbSubject,
Student = dbStudent.Student
});

}

await dbContext.SaveChangesAsync();
}


public async Task<List<Student>> GetSubjectStudentsAsync(Subject subject)
{
var students = await dbContext.AssignedSubjects
Expand Down
72 changes: 72 additions & 0 deletions SchoolManagerModel/Resources/UI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion SchoolManagerModel/Resources/UI.hu-HU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,33 @@
<value>A felhasználó nem található!</value>
</data>
<data name="EmailAddress" xml:space="preserve">
<value>Email address</value>
<value>Email cím</value>
</data>
<data name="Administrator" xml:space="preserve">
<value>Adminisztrátor</value>
</data>
<data name="SelectAll" xml:space="preserve">
<value>Összes kiválasztása</value>
</data>
<data name="UnselectAll" xml:space="preserve">
<value>Kijelölések törlése</value>
</data>
<data name="ID" xml:space="preserve">
<value>Azonosító</value>
</data>
<data name="Name" xml:space="preserve">
<value>Név</value>
</data>
<data name="MustBeALetter" xml:space="preserve">
<value>Betűnek kell lennie</value>
</data>
<data name="CanContainOnly1Letter" xml:space="preserve">
<value>Csak 1 betű szerepelhet benne</value>
</data>
<data name="MustBeNumber" xml:space="preserve">
<value>Számnak kell lennie</value>
</data>
<data name="MustBeGreaterThan0" xml:space="preserve">
<value>0-nál nagyobbnak kell lennie</value>
</data>
</root>
24 changes: 24 additions & 0 deletions SchoolManagerModel/Resources/UI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,28 @@
<data name="Administrator" xml:space="preserve">
<value>Administrator</value>
</data>
<data name="SelectAll" xml:space="preserve">
<value>Select all</value>
</data>
<data name="UnselectAll" xml:space="preserve">
<value>Unselect All</value>
</data>
<data name="ID" xml:space="preserve">
<value>ID</value>
</data>
<data name="Name" xml:space="preserve">
<value>Name</value>
</data>
<data name="MustBeALetter" xml:space="preserve">
<value>Must be a letter</value>
</data>
<data name="CanContainOnly1Letter" xml:space="preserve">
<value>Cam contain only one letter</value>
</data>
<data name="MustBeNumber" xml:space="preserve">
<value>Must be a number</value>
</data>
<data name="MustBeGreaterThan0" xml:space="preserve">
<value>Must be greater than 0</value>
</data>
</root>
24 changes: 24 additions & 0 deletions SchoolManagerModel/Validators/CharLetterValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using FluentValidation;
using System.Resources;

namespace SchoolManagerModel.Validators;

public class CharLetterValidator : AbstractValidator<string>
{
public CharLetterValidator(ResourceManager resourceManager)
{
RuleFor(x => x)
.Length(1)
.WithMessage(resourceManager.GetString("CanContainOnly1Letter")) // Check the length first
.DependentRules(() =>
{
RuleFor(x => x)
.Must(cls => cls.Length == 1 && Char.IsLetter(cls[0]))
.WithMessage(resourceManager.GetString("MustBeALetter"))
.When(cls => cls.Length == 1); // Ensure length is 1 before accessing the character
});



}
}
15 changes: 15 additions & 0 deletions SchoolManagerModel/Validators/ClassStringValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using FluentValidation;
using System.Resources;

namespace SchoolManagerModel.Validators;

public class ClassStringValidator : AbstractValidator<(string year, string cls)>
{
public ClassStringValidator(ResourceManager resourceManager)
{
RuleFor(x => x.year)
.SetValidator(new ClassYearStringValidator(resourceManager));
RuleFor(x => x.cls)
.SetValidator(new CharLetterValidator(resourceManager));
}
}
22 changes: 22 additions & 0 deletions SchoolManagerModel/Validators/ClassYearStringValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FluentValidation;
using System.Resources;

namespace SchoolManagerModel.Validators;

public class ClassYearStringValidator : AbstractValidator<string>
{
public ClassYearStringValidator(ResourceManager resourceManager)
{
RuleFor(x => x)
.SetValidator(new NumberValidator(resourceManager))
.DependentRules(() =>
{
// Check only if it is number
RuleFor(x => x)
.Must(x => int.TryParse(x, out var num) && num > 0)
.When(x => int.TryParse(x, out var _))
.WithMessage(resourceManager.GetString("MustBeGreaterThan0"));

});
}
}
14 changes: 14 additions & 0 deletions SchoolManagerModel/Validators/NumberValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using FluentValidation;
using System.Resources;

namespace SchoolManagerModel.Validators;

public class NumberValidator : AbstractValidator<string>
{
public NumberValidator(ResourceManager resourceManager)
{
RuleFor(a => a)
.Must(x => int.TryParse(x, out var val))
.WithMessage(resourceManager.GetString("MustBeNumber"));
}
}

0 comments on commit 1044b7b

Please sign in to comment.