-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Achievements #669
Merged
Merged
Achievements #669
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace OutOfSchool.Services.Models | ||
{ | ||
public class Achievement : IKeyedEntity<Guid> | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Text)] | ||
[MaxLength(2000)] | ||
[MinLength(1)] | ||
public string Title { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Date)] | ||
[Column(TypeName = "date")] | ||
public DateTime AchievementDate { get; set; } = default; | ||
|
||
[Required] | ||
public Guid WorkshopId { get; set; } | ||
|
||
public virtual Workshop Workshop { get; set; } | ||
|
||
[Required] | ||
public long AchievementTypeId { get; set; } | ||
|
||
public virtual AchievementType AchievementType { get; set; } | ||
|
||
public virtual List<Child> Children { get; set; } | ||
|
||
public virtual List<AchievementTeacher> Teachers { get; set; } | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
OutOfSchool/OutOfSchool.DataAccess/Models/AchievementTeacher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace OutOfSchool.Services.Models | ||
{ | ||
public class AchievementTeacher : IKeyedEntity<long> | ||
{ | ||
public long Id { get; set; } | ||
|
||
[Required] | ||
public Guid AchievementId { get; set; } | ||
|
||
public virtual Achievement Achievement { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Text)] | ||
[MaxLength(100)] | ||
[MinLength(1)] | ||
public string Title { get; set; } | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
OutOfSchool/OutOfSchool.DataAccess/Models/AchievementType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace OutOfSchool.Services.Models | ||
{ | ||
public class AchievementType : IKeyedEntity<long> | ||
{ | ||
public long Id { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Text)] | ||
[MaxLength(200)] | ||
[MinLength(1)] | ||
public string Title { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
OutOfSchool/OutOfSchool.DataAccess/Models/Configurations/AchievementConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace OutOfSchool.Services.Models.Configurations | ||
{ | ||
internal class AchievementConfiguration : IEntityTypeConfiguration<Achievement> | ||
{ | ||
public void Configure(EntityTypeBuilder<Achievement> builder) | ||
{ | ||
builder.HasKey(x => x.Id); | ||
|
||
builder.HasOne(x => x.AchievementType) | ||
.WithMany() | ||
.IsRequired() | ||
.HasForeignKey(x => x.AchievementTypeId) | ||
.OnDelete(DeleteBehavior.NoAction); | ||
|
||
builder.HasMany(x => x.Children) | ||
.WithMany(x => x.Achievements); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
OutOfSchool/OutOfSchool.DataAccess/Repository/AchievementRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using OutOfSchool.Services.Models; | ||
|
||
namespace OutOfSchool.Services.Repository | ||
{ | ||
/// <summary> | ||
/// Repository for accessing the Achievement table in database. | ||
/// </summary> | ||
public class AchievementRepository : EntityRepositoryBase<Guid, Achievement>, IAchievementRepository | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AchievementRepository"/> class. | ||
/// </summary> | ||
/// <param name="dbContext">OutOfSchoolDbContext.</param> | ||
public AchievementRepository(OutOfSchoolDbContext dbContext) | ||
: base(dbContext) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Get elements by Workshop Id. | ||
/// </summary> | ||
/// <param name="workshopId">GUID Workshop Id.</param> | ||
/// /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> | ||
public async Task<IEnumerable<Achievement>> GetByWorkshopId(Guid workshopId) | ||
{ | ||
var achievements = dbSet.Where(a => a.WorkshopId == workshopId); | ||
|
||
return await Task.FromResult(achievements); | ||
} | ||
|
||
/// <summary> | ||
/// Add new element. | ||
/// </summary> | ||
/// <param name="achievement">Entity to create.</param> | ||
/// <param name="childrenIDs">GUID List of Children.</param> | ||
/// <param name="teachers">String List of Teachers.</param> | ||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> | ||
public async Task<Achievement> Create(Achievement achievement, List<Guid> childrenIDs, List<string> teachers) | ||
{ | ||
achievement.Children = dbContext.Children.Where(w => childrenIDs.Contains(w.Id)) | ||
.ToList(); | ||
|
||
achievement.Teachers = new List<AchievementTeacher>(); | ||
|
||
foreach (string teacher in teachers) | ||
{ | ||
achievement.Teachers.Add(new AchievementTeacher { Title = teacher, Achievement = achievement }); | ||
} | ||
|
||
await dbSet.AddAsync(achievement); | ||
await dbContext.SaveChangesAsync(); | ||
|
||
return await Task.FromResult(achievement); | ||
} | ||
|
||
/// <summary> | ||
/// Update information about element. | ||
/// </summary> | ||
/// <param name="achievement">Entity to update.</param> | ||
/// <param name="childrenIDs">GUID List of Children.</param> | ||
/// <param name="teachers">String List of Teachers.</param> | ||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> | ||
public async Task<Achievement> Update(Achievement achievement, List<Guid> childrenIDs, List<string> teachers) | ||
{ | ||
var newAchievement = dbSet.Find(achievement.Id); | ||
|
||
dbContext.Entry(newAchievement).CurrentValues.SetValues(achievement); | ||
|
||
newAchievement.Children.RemoveAll(x => !childrenIDs.Contains(x.Id)); | ||
var exceptChildrenIDs = childrenIDs.Where(p => newAchievement.Children.All(x => x.Id != p)); | ||
newAchievement.Children.AddRange(dbContext.Children.Where(w => exceptChildrenIDs.Contains(w.Id)).ToList()); | ||
|
||
newAchievement.Teachers.RemoveAll(x => !teachers.Contains(x.Title)); | ||
|
||
var exceptTeachers = teachers.Where(p => newAchievement.Teachers.All(x => !x.Title.Equals(p))); | ||
foreach (var teacher in exceptTeachers) | ||
{ | ||
newAchievement.Teachers.Add(new AchievementTeacher { Title = teacher, Achievement = newAchievement }); | ||
} | ||
|
||
dbContext.Entry(newAchievement).State = EntityState.Modified; | ||
|
||
await this.dbContext.SaveChangesAsync(); | ||
return newAchievement; | ||
} | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
OutOfSchool/OutOfSchool.DataAccess/Repository/IAchievementRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using OutOfSchool.Services.Models; | ||
|
||
namespace OutOfSchool.Services.Repository | ||
{ | ||
public interface IAchievementRepository : IEntityRepositoryBase<Guid, Achievement> | ||
{ | ||
Task<IEnumerable<Achievement>> GetByWorkshopId(Guid workshopId); | ||
|
||
Task<Achievement> Create(Achievement achievement, List<Guid> childrenIDs, List<string> teachers); | ||
|
||
Task<Achievement> Update(Achievement entity, List<Guid> childrenIDs, List<string> teachers); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add
AchievementTeacher : IKeyedEntity<long>
and the same forAchievementType