Skip to content
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 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,43 @@ public static void Seed(this ModelBuilder builder)
Description = "provider admin permissions",
}
);

builder.Entity<AchievementType>().HasData(
new AchievementType
{
Id = 1L,
Title = "Переможці міжнародних та всеукраїнських спортивних змагань (індивідуальних та командних)",
},
new AchievementType
{
Id = 2L,
Title = "Призери та учасники міжнародних, всеукраїнських та призери регіональних конкурсів і виставок наукових, технічних, дослідницьких, інноваційних, ІТ проектів",
},
new AchievementType
{
Id = 3L,
Title = "Реципієнти міжнародних грантів",
},
new AchievementType
{
Id = 4L,
Title = "Призери міжнародних культурних конкурсів та фестивалів",
},
new AchievementType
{
Id = 5L,
Title = "Соціально активні категорії учнів",
},
new AchievementType
{
Id = 6L,
Title = "Цифрові інструменти Google для закладів вищої та фахової передвищої освіти",
},
new AchievementType
{
Id = 7L,
Title = "Переможці та учасники олімпіад міжнародного та всеукраїнського рівнів",
});
}

/// <summary>
Expand Down
37 changes: 37 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Achievement.cs
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 OutOfSchool/OutOfSchool.DataAccess/Models/AchievementTeacher.cs
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; }
Copy link

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 for AchievementType


[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 OutOfSchool/OutOfSchool.DataAccess/Models/AchievementType.cs
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; }
}
}
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Child.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ public class Child : IKeyedEntity<Guid>
public virtual ICollection<SocialGroup> SocialGroups { get; set; }

public virtual List<ChildSocialGroup> ChildSocialGroups { get; set; }

public virtual List<Achievement> Achievements { get; set; }
}
}
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);
}
}
}
7 changes: 7 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public OutOfSchoolDbContext(DbContextOptions<OutOfSchoolDbContext> options)

public DbSet<Codeficator> Codeficators { get; set; }

public DbSet<AchievementType> AchievementTypes { get; set; }

public DbSet<AchievementTeacher> AchievementTeachers { get; set; }

public DbSet<Achievement> Achievements { get; set; }

public async Task<int> CompleteAsync() => await this.SaveChangesAsync();

public int Complete() => this.SaveChanges();
Expand All @@ -116,6 +122,7 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.ApplyConfiguration(new WorkshopConfiguration());
builder.ApplyConfiguration(new EntityImagesConfiguration<Workshop>());
builder.ApplyConfiguration(new NotificationConfiguration());
builder.ApplyConfiguration(new AchievementConfiguration());

builder.Seed();
builder.UpdateIdentityTables();
Expand Down
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;
}

}
}
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);
}
}
Loading