Отправлю пока то Что успел

This commit is contained in:
Kirill 2024-04-30 23:50:39 +04:00
parent 5cec4e59ac
commit 78516eef25
8 changed files with 188 additions and 63 deletions

View File

@ -0,0 +1,82 @@
using Microsoft.EntityFrameworkCore;
using SchoolContracts.BindingModels;
using SchoolContracts.SearchModels;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolDatabaseImplement.Implements
{
public class LessonStorage : ILessonStorage
{
public List<LessonViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Lessons.Include(x => x.Achievements)
.Select(x => x.GetViewModel)
.ToList();
}
public List<LessonViewModel> GetFilteredList(LessonSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new SchoolDatabase();
return context.Lessons.Include(x => x.Achievements)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public LessonViewModel? GetElement(LessonSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new SchoolDatabase();
return context.Lessons.Include(x => x.Achievements).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public LessonViewModel? Insert(LessonBindingModel model)
{
var newLesson = Lesson.Create(model);
if (newLesson == null)
{
return null;
}
using var context = new SchoolDatabase();
context.Lessons.Add(newLesson);
context.SaveChanges();
return newLesson.GetViewModel;
}
public LessonViewModel? Update(LessonBindingModel model)
{
using var context = new SchoolDatabase();
var Lesson = context.Lessons.Include(x => x.Achievements).FirstOrDefault(x => x.Id == model.Id);
if (Lesson == null)
{
return null;
}
Lesson.Update(model);
context.SaveChanges();
return Lesson.GetViewModel;
}
public LessonViewModel? Delete(LessonBindingModel model)
{
using var context = new SchoolDatabase();
var element = context.Lessons.Include(x => x.Achievements).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Lessons.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,84 @@
using Microsoft.EntityFrameworkCore;
using SchoolContracts.BindingModels;
using SchoolContracts.SearchModels;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolDatabaseImplement.Implements
{
public class UserStorage
{
public List<UserViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Users.Include(x => x.Lessons)
.Select(x => x.GetViewModel)
.ToList();
}
public List<UserViewModel> GetFilteredList(UserSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new SchoolDatabase();
return context.Users.Include(x => x.Lessons)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public UserViewModel? GetElement(UserSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) &&
!model.Id.HasValue)
{
return null;
}
using var context = new SchoolDatabase();
return context.Users.Include(x => x.Lessons).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public UserViewModel? Insert(UserBindingModel model)
{
var newUser = User.Create(model);
if (newUser == null)
{
return null;
}
using var context = new SchoolDatabase();
context.Users.Add(newUser);
context.SaveChanges();
return newUser.GetViewModel;
}
public UserViewModel? Update(UserBindingModel model)
{
using var context = new SchoolDatabase();
var user = context.Users.Include(x => x.Lessons).FirstOrDefault(x => x.Id == model.Id);
if (user == null)
{
return null;
}
user.Update(model);
context.SaveChanges();
return user.GetViewModel;
}
public UserViewModel? Delete(UserBindingModel model)
{
using var context = new SchoolDatabase();
var element = context.Users.Include(x => x.Lessons).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Users.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -12,7 +12,7 @@ using SchoolDatabaseImplement;
namespace SchoolDatabaseImplement.Migrations
{
[DbContext(typeof(SchoolDatabase))]
[Migration("20240430184208_InitialCreate")]
[Migration("20240430191557_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -33,9 +33,6 @@ namespace SchoolDatabaseImplement.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int?>("AchievementId")
.HasColumnType("int");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
@ -52,7 +49,7 @@ namespace SchoolDatabaseImplement.Migrations
b.HasKey("Id");
b.HasIndex("AchievementId");
b.HasIndex("LessonId");
b.ToTable("Achievements");
});
@ -156,13 +153,8 @@ namespace SchoolDatabaseImplement.Migrations
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int?>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Users");
});
@ -170,7 +162,9 @@ namespace SchoolDatabaseImplement.Migrations
{
b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson")
.WithMany("Achievements")
.HasForeignKey("AchievementId");
.HasForeignKey("LessonId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Lesson");
});
@ -197,7 +191,7 @@ namespace SchoolDatabaseImplement.Migrations
modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b =>
{
b.HasOne("SchoolDatabaseImplement.Models.User", "User")
.WithMany()
.WithMany("Lessons")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@ -205,13 +199,6 @@ namespace SchoolDatabaseImplement.Migrations
b.Navigation("User");
});
modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b =>
{
b.HasOne("SchoolDatabaseImplement.Models.User", null)
.WithMany("Users")
.HasForeignKey("UserId");
});
modelBuilder.Entity("SchoolDatabaseImplement.Models.Interest", b =>
{
b.Navigation("Lessons");
@ -226,7 +213,7 @@ namespace SchoolDatabaseImplement.Migrations
modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b =>
{
b.Navigation("Users");
b.Navigation("Lessons");
});
#pragma warning restore 612, 618
}

View File

@ -36,17 +36,11 @@ namespace SchoolDatabaseImplement.Migrations
BirthDate = table.Column<DateTime>(type: "datetime2", nullable: false),
Mail = table.Column<string>(type: "nvarchar(max)", nullable: false),
PhoneNumber = table.Column<string>(type: "nvarchar(max)", nullable: false),
Password = table.Column<string>(type: "nvarchar(max)", nullable: false),
UserId = table.Column<int>(type: "int", nullable: true)
Password = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
table.ForeignKey(
name: "FK_Users_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
@ -77,7 +71,6 @@ namespace SchoolDatabaseImplement.Migrations
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
LessonId = table.Column<int>(type: "int", nullable: false),
AchievementId = table.Column<int>(type: "int", nullable: true),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
ReceiptDate = table.Column<DateTime>(type: "datetime2", nullable: false)
@ -86,10 +79,11 @@ namespace SchoolDatabaseImplement.Migrations
{
table.PrimaryKey("PK_Achievements", x => x.Id);
table.ForeignKey(
name: "FK_Achievements_Lessons_AchievementId",
column: x => x.AchievementId,
name: "FK_Achievements_Lessons_LessonId",
column: x => x.LessonId,
principalTable: "Lessons",
principalColumn: "Id");
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
@ -119,9 +113,9 @@ namespace SchoolDatabaseImplement.Migrations
});
migrationBuilder.CreateIndex(
name: "IX_Achievements_AchievementId",
name: "IX_Achievements_LessonId",
table: "Achievements",
column: "AchievementId");
column: "LessonId");
migrationBuilder.CreateIndex(
name: "IX_InterestLessons_InterestId",
@ -137,11 +131,6 @@ namespace SchoolDatabaseImplement.Migrations
name: "IX_Lessons_UserId",
table: "Lessons",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_Users_UserId",
table: "Users",
column: "UserId");
}
/// <inheritdoc />

View File

@ -30,9 +30,6 @@ namespace SchoolDatabaseImplement.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int?>("AchievementId")
.HasColumnType("int");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("nvarchar(max)");
@ -49,7 +46,7 @@ namespace SchoolDatabaseImplement.Migrations
b.HasKey("Id");
b.HasIndex("AchievementId");
b.HasIndex("LessonId");
b.ToTable("Achievements");
});
@ -153,13 +150,8 @@ namespace SchoolDatabaseImplement.Migrations
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int?>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Users");
});
@ -167,7 +159,9 @@ namespace SchoolDatabaseImplement.Migrations
{
b.HasOne("SchoolDatabaseImplement.Models.Lesson", "Lesson")
.WithMany("Achievements")
.HasForeignKey("AchievementId");
.HasForeignKey("LessonId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Lesson");
});
@ -194,7 +188,7 @@ namespace SchoolDatabaseImplement.Migrations
modelBuilder.Entity("SchoolDatabaseImplement.Models.Lesson", b =>
{
b.HasOne("SchoolDatabaseImplement.Models.User", "User")
.WithMany()
.WithMany("Lessons")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@ -202,13 +196,6 @@ namespace SchoolDatabaseImplement.Migrations
b.Navigation("User");
});
modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b =>
{
b.HasOne("SchoolDatabaseImplement.Models.User", null)
.WithMany("Users")
.HasForeignKey("UserId");
});
modelBuilder.Entity("SchoolDatabaseImplement.Models.Interest", b =>
{
b.Navigation("Lessons");
@ -223,7 +210,7 @@ namespace SchoolDatabaseImplement.Migrations
modelBuilder.Entity("SchoolDatabaseImplement.Models.User", b =>
{
b.Navigation("Users");
b.Navigation("Lessons");
});
#pragma warning restore 612, 618
}

View File

@ -24,7 +24,7 @@ namespace SchoolDatabaseImplement.Models
public int UserId { get; private set; }
public virtual User? User { get; private set; }
[ForeignKey("AchievementId")]
[ForeignKey("LessonId")]
public virtual List<Achievement> Achievements { get; set; } = new();
public static Lesson? Create(LessonBindingModel model)
{

View File

@ -26,7 +26,7 @@ namespace SchoolDatabaseImplement.Models
public string Password { get; private set; } = string.Empty;
[ForeignKey("UserId")]
public virtual List<User> Users { get; set; } = new();
public virtual List<Lesson> Lessons { get; set; } = new();
public static User? Create(UserBindingModel model)
{
if (model == null)

View File

@ -6,10 +6,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />