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

Khasanov/TimeRanges #340

Merged
merged 24 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
13a3c7f
Removed Idea files from repo
Bogdan-Hasanov Jul 31, 2021
336732a
Added newtonsoftjson as json body deserializer
Bogdan-Hasanov Aug 3, 2021
669b312
Fix "Get provider by user id" is enumerated twice
Bogdan-Hasanov Aug 3, 2021
c7bc23c
Added Unit of work
Bogdan-Hasanov Sep 11, 2021
cf63fe3
Updated time ranges
Bogdan-Hasanov Sep 20, 2021
586fdc5
Move workshopId prop back to teacher
Bogdan-Hasanov Sep 20, 2021
74a9106
Fixed some tests
Bogdan-Hasanov Sep 20, 2021
77f7206
Upd DAl references
Bogdan-Hasanov Sep 20, 2021
1a974b4
Revert changes
Bogdan-Hasanov Sep 20, 2021
af55627
Remove migrations
Bogdan-Hasanov Sep 20, 2021
a637e9e
Single migration
Bogdan-Hasanov Sep 20, 2021
869206e
Single migration upd
Bogdan-Hasanov Sep 20, 2021
4595657
Remove days per week from SQL script
Bogdan-Hasanov Sep 20, 2021
879df33
Remove unused code
Bogdan-Hasanov Sep 20, 2021
6d564fe
Small fixes
Bogdan-Hasanov Sep 22, 2021
4bedc85
Removed workday entity in favor of bit mask
Bogdan-Hasanov Sep 24, 2021
a6aba73
Introduced keywords separator char const
Bogdan-Hasanov Sep 24, 2021
353b15b
Workshop service uses automapper everywhere
Bogdan-Hasanov Sep 24, 2021
f25d9d9
Remove migration
Bogdan-Hasanov Sep 27, 2021
6d025af
TimeRanges migration
Bogdan-Hasanov Sep 27, 2021
3e22294
Validate that no none is passed
Bogdan-Hasanov Sep 27, 2021
f4951bf
Synchronous complete in IUnitOfWork, newtonsoft enum seriliazation ev…
Bogdan-Hasanov Sep 28, 2021
af75b7a
DateTimeRanges are required
Bogdan-Hasanov Sep 28, 2021
a5f462b
Added todo
Bogdan-Hasanov Sep 28, 2021
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
3,217 changes: 1,595 additions & 1,622 deletions Config/SQLQuery_insert test values.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Config/initDbServer.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
docker run -d --name sql_server -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Oos-password1" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
docker run -d --name sql_server -e "ACCEPT_EULA=Y" -e MSSQL_COLLATION="Ukrainian_100_CI_AS" -e "SA_PASSWORD=Oos-password1" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
pause
4 changes: 4 additions & 0 deletions Config/initDbServer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

docker run -d --name sql_server -e "ACCEPT_EULA=Y" -e MSSQL_COLLATION="Ukrainian_100_CI_AS" -e "SA_PASSWORD=Oos-password1" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

13 changes: 0 additions & 13 deletions OutOfSchool/.idea/.idea.OutOfSchool/.idea/.gitignore

This file was deleted.

12 changes: 0 additions & 12 deletions OutOfSchool/.idea/.idea.OutOfSchool/.idea/dataSources.xml

This file was deleted.

4 changes: 0 additions & 4 deletions OutOfSchool/.idea/.idea.OutOfSchool/.idea/encodings.xml

This file was deleted.

8 changes: 0 additions & 8 deletions OutOfSchool/.idea/.idea.OutOfSchool/.idea/indexLayout.xml

This file was deleted.

6 changes: 0 additions & 6 deletions OutOfSchool/.idea/.idea.OutOfSchool/.idea/sqldialects.xml

This file was deleted.

6 changes: 0 additions & 6 deletions OutOfSchool/.idea/.idea.OutOfSchool/.idea/vcs.xml

This file was deleted.

5 changes: 3 additions & 2 deletions OutOfSchool/OutOfSchool.DataAccess/Enums/ApplicationStatus.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace OutOfSchool.Services.Enums
{
// TODO: Swagger ignores this attribute on model property in webapi layer
// TODO: Ask Frontend if we can just make all enums as strings instead of magic numbers
[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
public enum ApplicationStatus
{
Pending = 1,
Expand Down
20 changes: 20 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Enums/DaysBitMask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace OutOfSchool.Services.Enums
{
[JsonConverter(typeof(StringEnumConverter))]
[Flags]
public enum DaysBitMask
{
None = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64,
}
}
11 changes: 11 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace OutOfSchool.Services
{
public interface IUnitOfWork
{
public Task<int> CompleteAsync();

public int Complete();
}
}
21 changes: 21 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/DateTimeRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using OutOfSchool.Services.Enums;

namespace OutOfSchool.Services.Models
{
public class DateTimeRange
{
public long Id { get; set; }

public TimeSpan StartTime { get; set; }

public TimeSpan EndTime { get; set; }

public long WorkshopId { get; set; }

[Required]
public List<DaysBitMask> Workdays { get; set; }
}
}
7 changes: 3 additions & 4 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Workshop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json;

namespace OutOfSchool.Services.Models
{
Expand Down Expand Up @@ -47,10 +48,6 @@ public class Workshop
[Range(0, 18, ErrorMessage = "Max age should be a number from 0 to 18")]
public int MaxAge { get; set; }

[Required(ErrorMessage = "Specify how many times per week lessons will be held")]
[Range(1, 7, ErrorMessage = "Field should be a digit from 1 to 7")]
public int DaysPerWeek { get; set; }

[Column(TypeName = "decimal(18,2)")]
[Range(0, 10000, ErrorMessage = "Field value should be in a range from 1 to 10 000")]
public decimal Price { get; set; } = default;
Expand Down Expand Up @@ -113,6 +110,8 @@ public class Workshop

public virtual List<Application> Applications { get; set; }

public virtual List<DateTimeRange> DateTimeRanges { get; set; }

// These properties are only for navigation EF Core.
public virtual ICollection<ChatRoom> ChatRooms { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
46 changes: 35 additions & 11 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using OutOfSchool.Services.Enums;
using OutOfSchool.Services.Extensions;
using OutOfSchool.Services.Models;

namespace OutOfSchool.Services
{
public class OutOfSchoolDbContext : IdentityDbContext<User>, IDataProtectionKeyContext
public class OutOfSchoolDbContext : IdentityDbContext<User>, IDataProtectionKeyContext, IUnitOfWork
{
public OutOfSchoolDbContext(DbContextOptions<OutOfSchoolDbContext> options)
: base(options)
Expand Down Expand Up @@ -47,6 +53,12 @@ public OutOfSchoolDbContext(DbContextOptions<OutOfSchoolDbContext> options)

public DbSet<Favorite> Favorites { get; set; }

public DbSet<DateTimeRange> DateTimeRanges { get; set; }

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

public int Complete() => this.SaveChanges();

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
Expand All @@ -67,16 +79,16 @@ protected override void OnModelCreating(ModelBuilder builder)
.HasMany(r => r.Users)
.WithMany(u => u.ChatRooms)
.UsingEntity<ChatRoomUser>(
j => j
.HasOne(cru => cru.User)
.WithMany(u => u.ChatRoomUsers)
.HasForeignKey(cru => cru.UserId)
.OnDelete(DeleteBehavior.Cascade),
j => j
.HasOne(cru => cru.ChatRoom)
.WithMany(r => r.ChatRoomUsers)
.HasForeignKey(cru => cru.ChatRoomId)
.OnDelete(DeleteBehavior.Cascade));
j => j
.HasOne(cru => cru.User)
.WithMany(u => u.ChatRoomUsers)
.HasForeignKey(cru => cru.UserId)
.OnDelete(DeleteBehavior.Cascade),
j => j
.HasOne(cru => cru.ChatRoom)
.WithMany(r => r.ChatRoomUsers)
.HasForeignKey(cru => cru.ChatRoomId)
.OnDelete(DeleteBehavior.Cascade));

builder.Entity<ChatRoom>()
.HasOne(r => r.Workshop)
Expand Down Expand Up @@ -107,6 +119,18 @@ protected override void OnModelCreating(ModelBuilder builder)
.HasForeignKey(x => x.ActualAddressId)
.IsRequired(false);

builder.Entity<DateTimeRange>()
.HasCheckConstraint("CK_DateTimeRanges_EndTimeIsAfterStartTime", "[EndTime] >= [StartTime]");

builder.Entity<DateTimeRange>()
.Property(range => range.Workdays)
.HasConversion(
list => (byte)list.Aggregate((prev, next) => prev | next),
mask =>
Enum.GetValues(typeof(DaysBitMask)).Cast<DaysBitMask>().ToList()
.Where(amenity => amenity != 0 && ((DaysBitMask) mask).HasFlag(amenity)).ToList(),
ValueComparer.CreateDefault(typeof(List<DaysBitMask>), true));

builder.Seed();
builder.UpdateIdentityTables();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ namespace OutOfSchool.Services.Repository
{
public interface IWorkshopRepository : IEntityRepository<Workshop>
{
IUnitOfWork UnitOfWork { get; }

/// <summary>
/// Checks entity classId existence.
/// </summary>
/// <param name="id">Class id.</param>
/// <returns>True if Class exists, otherwise false.</returns>
bool ClassExists(long id);

Task<Workshop> GetWithNavigations(long id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using OutOfSchool.Services.Models;
Expand All @@ -13,11 +10,13 @@ public class WorkshopRepository : EntityRepository<Workshop>, IWorkshopRepositor
private readonly OutOfSchoolDbContext db;

public WorkshopRepository(OutOfSchoolDbContext dbContext)
: base(dbContext)
: base(dbContext)
{
db = dbContext;
}

public IUnitOfWork UnitOfWork => db;

/// <inheritdoc/>
public new async Task Delete(Workshop entity)
{
Expand All @@ -43,6 +42,15 @@ public WorkshopRepository(OutOfSchoolDbContext dbContext)
await db.SaveChangesAsync();
}

public async Task<Workshop> GetWithNavigations(long id)
{
return await db.Workshops
.Include(ws => ws.Address)
.Include(ws => ws.Teachers)
.Include(ws => ws.DateTimeRanges)
.SingleOrDefaultAsync(ws => ws.Id == id);
}

/// <inheritdoc/>
public bool ClassExists(long id) => db.Classes.Any(x => x.Id == id);
}
Expand Down
Loading