-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d368e2b
commit 9d5cccc
Showing
12 changed files
with
2,579 additions
and
86 deletions.
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
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.