diff --git a/School/Program.cs b/School/Program.cs new file mode 100644 index 0000000..b9debd1 --- /dev/null +++ b/School/Program.cs @@ -0,0 +1,131 @@ +namespace School; + +public class SchoolMain +{ + public static void Main(string[] args) + { + SchoolContext database = new SchoolContext(); + database.Database.EnsureDeleted(); + database.Database.EnsureCreated(); + + database.Teachers.Add(new Teacher { Name = "Rosana" }); + database.Teachers.Add(new Teacher { Name = "Pedro" }); + database.SaveChanges(); + + StartProgram: + + Console.WriteLine("Menu:"); + Console.WriteLine("1 - Visualizar dados"); + Console.WriteLine("2 - Inserir dados"); + Console.WriteLine("3 - Atualizar dados"); + Console.WriteLine("4 - Remover dados"); + Console.WriteLine("5 - Visualizar pessoas educadoras"); + + int option = int.Parse(Console.ReadLine()!); + + switch(option) { + case 1: + SelectStudents(database); + goto StartProgram; + case 2: + InsertStudents(database); + goto StartProgram; + case 3: + UpdateStudent(database); + goto StartProgram; + case 4: + DeleteStudent(database); + goto StartProgram; + case 5: + SelectTeachers(database); + goto StartProgram; + + } + + + } + + public static void SelectStudents( SchoolContext database) + { + Console.Clear(); + foreach(Student student in database.Students) + { + Console.WriteLine($"{student.Id} - {student.Name} - {student.Course} - Pessoa Educadora: {student.Teacher.Name}"); + } + } + + + public static void SelectTeachers( SchoolContext database) + { + Console.Clear(); + foreach(Teacher teacher in database.Teachers) + { + Console.WriteLine($"{teacher.Id} - {teacher.Name}"); + Console.WriteLine("Pessoas estudante"); + foreach(Student student in teacher.Students) + { + Console.WriteLine(student.Name); + } + Console.WriteLine(""); + } + } + + public static void InsertStudents( SchoolContext database) + { + Console.Clear(); + Console.WriteLine("Digite o nome da pessoa estudante"); + string name = Console.ReadLine()!; + + Console.WriteLine("Digite o curso"); + string course = Console.ReadLine()!; + + Console.WriteLine("Digite o id da pessoa educadora"); + int teacherId = int.Parse(Console.ReadLine()); + + Student newStudent = new Student + { + Name = name, + Course = course, + TeacherId = teacherId + }; + database.Students.Add(newStudent); + database.SaveChanges(); + } + + public static void UpdateStudent(SchoolContext database) + { + Console.Clear(); + Console.WriteLine("Informe o Id da Pessoa Estudante"); + int id = int.Parse(Console.ReadLine()); + + Console.WriteLine("Digite o nome da pessoa estudante"); + string name = Console.ReadLine()!; + + Console.WriteLine("Digite o curso"); + string course = Console.ReadLine()!; + + Console.WriteLine("Digite o id da pessoa educadora"); + int teacherId = int.Parse(Console.ReadLine()); + + Student existingStudent = database.Students.Find(id); + existingStudent.Name = name; + existingStudent.Course = course; + existingStudent.TeacherId = teacherId; + database.Students.Update(existingStudent); + database.SaveChanges(); + + + } + + public static void DeleteStudent( SchoolContext database) + { + Console.Clear(); + Console.WriteLine("Informe o Id da Pessoa Estudante"); + int id = int.Parse(Console.ReadLine()); + + Student existingStudent = database.Students.Find(id); + database.Students.Remove(existingStudent); + database.SaveChanges(); + } + +} \ No newline at end of file diff --git a/School/School.csproj b/School/School.csproj new file mode 100644 index 0000000..eb0be4d --- /dev/null +++ b/School/School.csproj @@ -0,0 +1,19 @@ + + + + Exe + net6.0 + enable + enable + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + diff --git a/School/SchoolContext.cs b/School/SchoolContext.cs new file mode 100644 index 0000000..24c5f64 --- /dev/null +++ b/School/SchoolContext.cs @@ -0,0 +1,21 @@ +namespace School; +using Microsoft.EntityFrameworkCore; + +public class SchoolContext : DbContext +{ + public SchoolContext(DbContextOptions options) : base(options) {} + public SchoolContext() {} + + public DbSet Students{ get; set; } = null!; + public DbSet Teachers { get; set; } = null!; + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + var connectionString = "Server=localhost;Database=School;User=SA;Password=SqlServer123!;TrustServerCertificate=True"; + optionsBuilder.UseSqlServer(connectionString); + } + } + +} \ No newline at end of file diff --git a/School/Student.cs b/School/Student.cs new file mode 100644 index 0000000..29ddeff --- /dev/null +++ b/School/Student.cs @@ -0,0 +1,14 @@ +namespace School; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +public class Student +{ + [Key] + public int Id { get; set;} + public string? Name { get; set; } + public string? Course { get; set;} + [ForeignKey("TeacherId")] + public int TeacherId { get; set; } + public Teacher? Teacher { get; set; } +} \ No newline at end of file diff --git a/School/Teacher.cs b/School/Teacher.cs new file mode 100644 index 0000000..2949e05 --- /dev/null +++ b/School/Teacher.cs @@ -0,0 +1,10 @@ +namespace School; +using System.ComponentModel.DataAnnotations; + +public class Teacher +{ + [Key] + public int Id { get; set; } + public string? Name { get; set; } + public ICollection? Students { get; set; } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e802fdf --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' +services: + db: + image: mcr.microsoft.com/azure-sql-edge:latest + container_name: my_sql_server + ports: + - 1433:1433 + environment: + - ACCEPT_EULA=Y + - MSSQL_SA_PASSWORD=SqlServer123! + - MSSQL_PID=Developer \ No newline at end of file diff --git a/sqlserver.sln b/sqlserver.sln new file mode 100644 index 0000000..5b3db48 --- /dev/null +++ b/sqlserver.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "School", "School\School.csproj", "{4B8AD814-8738-48B0-AECF-3701278325BD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4B8AD814-8738-48B0-AECF-3701278325BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B8AD814-8738-48B0-AECF-3701278325BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B8AD814-8738-48B0-AECF-3701278325BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B8AD814-8738-48B0-AECF-3701278325BD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A3FAFF39-CF24-4D86-9E01-106628DDC79F} + EndGlobalSection +EndGlobal