Skip to content

Commit 5fc7ba7

Browse files
committed
Adicionado src do primeiro modulo
1 parent b10aae9 commit 5fc7ba7

12 files changed

+720
-0
lines changed

.gitignore

+454
Large diffs are not rendered by default.

src/Modulo 1/Blog/Blog.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.9" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
11+
</ItemGroup>
12+
13+
</Project>
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Blog.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace Blog.Data
5+
{
6+
public class BlogDataContext : DbContext
7+
{
8+
public DbSet<Category> Categories { get; set; }
9+
public DbSet<Post> Posts { get; set; }
10+
// public DbSet<PostTag> PostTags { get; set; }
11+
public DbSet<Role> Roles { get; set; }
12+
public DbSet<Tag> Tags { get; set; }
13+
public DbSet<User> Users { get; set; }
14+
// public DbSet<UserRole> UserRoles { get; set; }
15+
16+
protected override void OnConfiguring(DbContextOptionsBuilder options)
17+
=> options.UseSqlServer("Server=localhost,1433;Database=Blog;User ID=sa;Password=1q2w3e4r@#$");
18+
}
19+
}

src/Modulo 1/Blog/Models/Category.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Blog.Models
2+
{
3+
public class Category
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public string Slug { get; set; }
8+
}
9+
}

src/Modulo 1/Blog/Models/Post.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Blog.Models
4+
{
5+
public class Post
6+
{
7+
public int Id { get; set; }
8+
public int CategoryId { get; set; }
9+
public int AuthorId { get; set; }
10+
public string Title { get; set; }
11+
public string Summary { get; set; }
12+
public string Body { get; set; }
13+
public string Slug { get; set; }
14+
public DateTime CreateDate { get; set; }
15+
public DateTime LastUpdateDate { get; set; }
16+
}
17+
}

src/Modulo 1/Blog/Models/PostTag.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Blog.Models
2+
{
3+
public class PostTag
4+
{
5+
public int PostId { get; set; }
6+
public int TagId { get; set; }
7+
}
8+
}

src/Modulo 1/Blog/Models/Role.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Blog.Models
2+
{
3+
public class Role
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public string Slug { get; set; }
8+
}
9+
}

src/Modulo 1/Blog/Models/Tag.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
4+
namespace Blog.Models
5+
{
6+
[Table("Tag")]
7+
public class Tag
8+
{
9+
[Key]
10+
public int Id { get; set; }
11+
public string Name { get; set; }
12+
public string Slug { get; set; }
13+
}
14+
}

src/Modulo 1/Blog/Models/User.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Blog.Models
2+
{
3+
public class User
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public string Email { get; set; }
8+
public string PasswordHash { get; set; }
9+
public string Image { get; set; }
10+
public string Slug { get; set; }
11+
public string Bio { get; set; }
12+
}
13+
}

src/Modulo 1/Blog/Models/UserRole.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Blog.Models
2+
{
3+
public class UserRole
4+
{
5+
public int UserId { get; set; }
6+
public int RoleId { get; set; }
7+
}
8+
}

src/Modulo 1/Blog/Program.cs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Linq;
3+
using Blog.Data;
4+
using Blog.Models;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace Blog
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
using (var context = new BlogDataContext())
14+
{
15+
// var tag = new Tag { Name = ".NET", Slug = "dotnet" };
16+
// context.Tags.Add(tag);
17+
// context.SaveChanges();
18+
19+
// var tag2 = new Tag { Name = "ASP.NET", Slug = "aspnet" };
20+
// context.Tags.Add(tag2);
21+
// context.SaveChanges();
22+
23+
// var tag = context.Tags.FirstOrDefault(x => x.Id == 2);
24+
// tag.Name = ".NET";
25+
// tag.Slug = "dotnet";
26+
27+
// context.Update(tag);
28+
// context.SaveChanges();
29+
30+
// var tag = context.Tags.FirstOrDefault(x => x.Id == 2);
31+
32+
// context.Remove(tag);
33+
// context.SaveChanges();
34+
35+
// var tags = context
36+
// .Tags
37+
// .AsNoTracking()
38+
// .ToList();
39+
40+
// foreach (var tag in tags)
41+
// {
42+
// Console.WriteLine(tag.Name);
43+
// }
44+
45+
// var tag = context
46+
// .Tags
47+
// .AsNoTracking() // REMOVER
48+
// .FirstOrDefault(x => x.Id == 3);
49+
50+
// tag.Name = "Ponto NET";
51+
// tag.Slug = "dotnet";
52+
53+
// context.Update(tag);
54+
// context.SaveChanges();
55+
56+
// var tag = context
57+
// .Tags
58+
// .AsNoTracking()
59+
// .FirstOrDefault(x => x.Id == 3);
60+
61+
// Console.WriteLine(tag?.Name);
62+
}
63+
}
64+
}
65+
}

src/Modulo 1/Scripts/blog.sql.txt

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
CREATE DATABASE [Blog]
2+
GO
3+
4+
USE [Blog]
5+
GO
6+
7+
-- DROP TABLE [User]
8+
-- DROP TABLE [Role]
9+
-- DROP TABLE [UserRole]
10+
-- DROP TABLE [Post]
11+
-- DROP TABLE [Category]
12+
-- DROP TABLE [Tag]
13+
-- DROP TABLE [PostTag]
14+
15+
CREATE TABLE [User] (
16+
[Id] INT NOT NULL IDENTITY(1, 1),
17+
[Name] NVARCHAR(80) NOT NULL,
18+
[Email] VARCHAR(200) NOT NULL,
19+
[PasswordHash] VARCHAR(255) NOT NULL,
20+
[Bio] TEXT NOT NULL,
21+
[Image] VARCHAR(2000) NOT NULL,
22+
[Slug] VARCHAR(80) NOT NULL,
23+
24+
CONSTRAINT [PK_User] PRIMARY KEY([Id]),
25+
CONSTRAINT [UQ_User_Email] UNIQUE([Email]),
26+
CONSTRAINT [UQ_User_Slug] UNIQUE([Slug])
27+
)
28+
CREATE NONCLUSTERED INDEX [IX_User_Email] ON [User]([Email])
29+
CREATE NONCLUSTERED INDEX [IX_User_Slug] ON [User]([Slug])
30+
31+
CREATE TABLE [Role] (
32+
[Id] INT NOT NULL IDENTITY(1, 1),
33+
[Name] VARCHAR(80) NOT NULL,
34+
[Slug] VARCHAR(80) NOT NULL,
35+
36+
CONSTRAINT [PK_Role] PRIMARY KEY([Id]),
37+
CONSTRAINT [UQ_Role_Slug] UNIQUE([Slug])
38+
)
39+
CREATE NONCLUSTERED INDEX [IX_Role_Slug] ON [Role]([Slug])
40+
41+
CREATE TABLE [UserRole] (
42+
[UserId] INT NOT NULL,
43+
[RoleId] INT NOT NULL,
44+
45+
CONSTRAINT [PK_UserRole] PRIMARY KEY([UserId], [RoleId])
46+
)
47+
48+
CREATE TABLE [Category] (
49+
[Id] INT NOT NULL IDENTITY(1, 1),
50+
[Name] VARCHAR(80) NOT NULL,
51+
[Slug] VARCHAR(80) NOT NULL,
52+
53+
CONSTRAINT [PK_Category] PRIMARY KEY([Id]),
54+
CONSTRAINT [UQ_Category_Slug] UNIQUE([Slug])
55+
)
56+
CREATE NONCLUSTERED INDEX [IX_Category_Slug] ON [Category]([Slug])
57+
58+
CREATE TABLE [Post] (
59+
[Id] INT NOT NULL IDENTITY(1, 1),
60+
[CategoryId] INT NOT NULL,
61+
[AuthorId] INT NOT NULL,
62+
[Title] VARCHAR(160) NOT NULL,
63+
[Summary] VARCHAR(255) NOT NULL,
64+
[Body] TEXT NOT NULL,
65+
[Slug] VARCHAR(80) NOT NULL,
66+
[CreateDate] DATETIME NOT NULL DEFAULT(GETDATE()),
67+
[LastUpdateDate] DATETIME NOT NULL DEFAULT(GETDATE()),
68+
69+
CONSTRAINT [PK_Post] PRIMARY KEY([Id]),
70+
CONSTRAINT [FK_Post_Category] FOREIGN KEY([CategoryId]) REFERENCES [Category]([Id]),
71+
CONSTRAINT [FK_Post_Author] FOREIGN KEY([AuthorId]) REFERENCES [User]([Id]),
72+
CONSTRAINT [UQ_Post_Slug] UNIQUE([Slug])
73+
)
74+
CREATE NONCLUSTERED INDEX [IX_Post_Slug] ON [Post]([Slug])
75+
76+
CREATE TABLE [Tag] (
77+
[Id] INT NOT NULL IDENTITY(1, 1),
78+
[Name] VARCHAR(80) NOT NULL,
79+
[Slug] VARCHAR(80) NOT NULL,
80+
81+
CONSTRAINT [PK_Tag] PRIMARY KEY([Id]),
82+
CONSTRAINT [UQ_Tag_Slug] UNIQUE([Slug])
83+
)
84+
CREATE NONCLUSTERED INDEX [IX_Tag_Slug] ON [Tag]([Slug])
85+
86+
CREATE TABLE [PostTag] (
87+
[PostId] INT NOT NULL,
88+
[TagId] INT NOT NULL,
89+
90+
CONSTRAINT PK_PostTag PRIMARY KEY([PostId], [TagId])
91+
)

0 commit comments

Comments
 (0)