diff --git a/Prova2_Henrique/Prova/Controllers/MarcasController.cs b/Prova2_Henrique/Prova/Controllers/MarcasController.cs index 1f73dea..c7b7e8a 100644 --- a/Prova2_Henrique/Prova/Controllers/MarcasController.cs +++ b/Prova2_Henrique/Prova/Controllers/MarcasController.cs @@ -1,8 +1,6 @@ using Microsoft.AspNetCore.Mvc; using Prova.Models; using Prova.Services; -using System.Collections.Generic; -using System.Threading.Tasks; namespace Prova.Controllers { @@ -18,14 +16,14 @@ public MarcaController(MarcaService marcaService) } [HttpGet] - public async Task>> GetAllMarcas() + public async Task>> GetTodasMarcas() { var marcas = await _marcaService.GetAllAsync(); return Ok(marcas); } [HttpGet("{id}")] - public async Task> GetMarcaById(int id) + public async Task> GetMarcaPorId(int id) { var marca = await _marcaService.GetByIdAsync(id); if (marca == null) @@ -38,8 +36,8 @@ public async Task> GetMarcaById(int id) [HttpPost] public async Task> CreateMarca(Marca marca) { - var newMarca = await _marcaService.AddAsync(marca); - return CreatedAtAction(nameof(GetMarcaById), new { id = newMarca.Id }, newMarca); + var novaMarca = await _marcaService.AddAsync(marca); + return CreatedAtAction(nameof(GetMarcaPorId), new { id = novaMarca.Id }, novaMarca); } } } diff --git a/Prova2_Henrique/Prova/Controllers/ProduotsController.cs b/Prova2_Henrique/Prova/Controllers/ProduotsController.cs index df396cf..9e6f404 100644 --- a/Prova2_Henrique/Prova/Controllers/ProduotsController.cs +++ b/Prova2_Henrique/Prova/Controllers/ProduotsController.cs @@ -18,14 +18,14 @@ public ProdutoController(ProdutoService produtoService) } [HttpGet] - public async Task>> GetAllProdutos() + public async Task>> GetTodosProdutos() { var produtos = await _produtoService.GetAllAsync(); return Ok(produtos); } [HttpGet("{id}")] - public async Task> GetProdutoById(int id) + public async Task> GetProdutoPorId(int id) { var produto = await _produtoService.GetByIdAsync(id); if (produto == null) @@ -38,8 +38,8 @@ public async Task> GetProdutoById(int id) [HttpPost] public async Task> CreateProduto(Produto produto) { - var newProduto = await _produtoService.AddAsync(produto); - return CreatedAtAction(nameof(GetProdutoById), new { id = newProduto.Id }, newProduto); + var novoProduto = await _produtoService.AddAsync(produto); + return CreatedAtAction(nameof(GetProdutoPorId), new { id = novoProduto.Id }, novoProduto); } [HttpDelete] diff --git a/Prova2_Henrique/Prova/Controllers/UsuarioController.cs b/Prova2_Henrique/Prova/Controllers/UsuarioController.cs index f66962c..717897f 100644 --- a/Prova2_Henrique/Prova/Controllers/UsuarioController.cs +++ b/Prova2_Henrique/Prova/Controllers/UsuarioController.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Mvc; using Prova.DTOs; -using Prova.Models; using Prova.Services; namespace Prova.Controllers diff --git a/Prova2_Henrique/Prova/DTO/EnderecoDTO.cs b/Prova2_Henrique/Prova/DTO/EnderecoDTO.cs new file mode 100644 index 0000000..370f75b --- /dev/null +++ b/Prova2_Henrique/Prova/DTO/EnderecoDTO.cs @@ -0,0 +1,10 @@ +namespace Prova.DTOs +{ + public class EnderecoDTO + { + public string Cidade { get; set; } = string.Empty; + public string Estado { get; set; } = string.Empty; + public string Cep { get; set; } = string.Empty; + public int UsuarioId { get; set; } + } +} \ No newline at end of file diff --git a/Prova2_Henrique/Prova/DTO/UsuarioAtualizacaoDTO.cs b/Prova2_Henrique/Prova/DTO/UsuarioAtualizacaoDTO.cs index fb9633c..c98f5b8 100644 --- a/Prova2_Henrique/Prova/DTO/UsuarioAtualizacaoDTO.cs +++ b/Prova2_Henrique/Prova/DTO/UsuarioAtualizacaoDTO.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using Prova.Models; namespace Prova.DTOs { diff --git a/Prova2_Henrique/Prova/DTO/UsuarioCadastroDTO.cs b/Prova2_Henrique/Prova/DTO/UsuarioCadastroDTO.cs index 23e86fb..7d727aa 100644 --- a/Prova2_Henrique/Prova/DTO/UsuarioCadastroDTO.cs +++ b/Prova2_Henrique/Prova/DTO/UsuarioCadastroDTO.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json.Serialization; -using Prova.Models; namespace Prova.DTOs { diff --git a/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.Designer.cs b/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.Designer.cs index 1dbce19..d51a73b 100644 --- a/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.Designer.cs +++ b/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.Designer.cs @@ -12,8 +12,13 @@ namespace Prova.Migrations { [DbContext(typeof(AppDbContext))] +<<<<<<<< HEAD:Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.Designer.cs + [Migration("20241129233214_euteamoluquinhas")] + partial class euteamoluquinhas +======== [Migration("20241129230149_InitialCreate")] partial class InitialCreate +>>>>>>>> 08cc1bad6ec5360a42e1bfe921c40f574ac1bc8a:Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.Designer.cs { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.cs b/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.cs index b878bcd..72abd08 100644 --- a/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.cs +++ b/Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.cs @@ -6,7 +6,11 @@ namespace Prova.Migrations { /// +<<<<<<<< HEAD:Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.cs + public partial class euteamoluquinhas : Migration +======== public partial class InitialCreate : Migration +>>>>>>>> 08cc1bad6ec5360a42e1bfe921c40f574ac1bc8a:Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.cs { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.Designer.cs b/Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.Designer.cs new file mode 100644 index 0000000..d51a73b --- /dev/null +++ b/Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.Designer.cs @@ -0,0 +1,183 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Prova.Data; + +#nullable disable + +namespace Prova.Migrations +{ + [DbContext(typeof(AppDbContext))] +<<<<<<<< HEAD:Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.Designer.cs + [Migration("20241129233214_euteamoluquinhas")] + partial class euteamoluquinhas +======== + [Migration("20241129230149_InitialCreate")] + partial class InitialCreate +>>>>>>>> 08cc1bad6ec5360a42e1bfe921c40f574ac1bc8a:Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.Designer.cs + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Prova.Models.Endereco", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cep") + .HasColumnType("nvarchar(max)"); + + b.Property("Cidade") + .HasColumnType("nvarchar(max)"); + + b.Property("Estado") + .HasColumnType("nvarchar(max)"); + + b.Property("UsuarioId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("UsuarioId"); + + b.ToTable("Enderecos"); + }); + + modelBuilder.Entity("Prova.Models.Marca", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Nome") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Marcas"); + }); + + modelBuilder.Entity("Prova.Models.Produto", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Descricao") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IdMarca") + .HasColumnType("int"); + + b.Property("MarcaId") + .HasColumnType("int"); + + b.Property("Nome") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Preco") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("IdMarca"); + + b.HasIndex("MarcaId"); + + b.ToTable("Produtos"); + }); + + modelBuilder.Entity("Usuario", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cep") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cpf") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DataNascimento") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Nome") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Senha") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Sobrenome") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Telefone") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Usuarios"); + }); + + modelBuilder.Entity("Prova.Models.Endereco", b => + { + b.HasOne("Usuario", "Usuario") + .WithMany() + .HasForeignKey("UsuarioId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Usuario"); + }); + + modelBuilder.Entity("Prova.Models.Produto", b => + { + b.HasOne("Prova.Models.Marca", null) + .WithMany() + .HasForeignKey("IdMarca") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Prova.Models.Marca", "Marca") + .WithMany() + .HasForeignKey("MarcaId"); + + b.Navigation("Marca"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.cs b/Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.cs new file mode 100644 index 0000000..72abd08 --- /dev/null +++ b/Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.cs @@ -0,0 +1,133 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Prova.Migrations +{ + /// +<<<<<<<< HEAD:Prova2_Henrique/Prova/Migrations/20241129233214_euteamoluquinhas.cs + public partial class euteamoluquinhas : Migration +======== + public partial class InitialCreate : Migration +>>>>>>>> 08cc1bad6ec5360a42e1bfe921c40f574ac1bc8a:Prova2_Henrique/Prova/Migrations/20241129230149_InitialCreate.cs + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Marcas", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Nome = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Marcas", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Usuarios", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Nome = table.Column(type: "nvarchar(max)", nullable: false), + Sobrenome = table.Column(type: "nvarchar(max)", nullable: false), + Telefone = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false), + Senha = table.Column(type: "nvarchar(max)", nullable: false), + Cpf = table.Column(type: "nvarchar(max)", nullable: false), + Cep = table.Column(type: "nvarchar(max)", nullable: false), + DataNascimento = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Usuarios", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Produtos", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Nome = table.Column(type: "nvarchar(max)", nullable: false), + Descricao = table.Column(type: "nvarchar(max)", nullable: false), + Preco = table.Column(type: "nvarchar(max)", nullable: false), + IdMarca = table.Column(type: "int", nullable: false), + MarcaId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Produtos", x => x.Id); + table.ForeignKey( + name: "FK_Produtos_Marcas_IdMarca", + column: x => x.IdMarca, + principalTable: "Marcas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Produtos_Marcas_MarcaId", + column: x => x.MarcaId, + principalTable: "Marcas", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "Enderecos", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Cep = table.Column(type: "nvarchar(max)", nullable: true), + Cidade = table.Column(type: "nvarchar(max)", nullable: true), + Estado = table.Column(type: "nvarchar(max)", nullable: true), + UsuarioId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Enderecos", x => x.Id); + table.ForeignKey( + name: "FK_Enderecos_Usuarios_UsuarioId", + column: x => x.UsuarioId, + principalTable: "Usuarios", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Enderecos_UsuarioId", + table: "Enderecos", + column: "UsuarioId"); + + migrationBuilder.CreateIndex( + name: "IX_Produtos_IdMarca", + table: "Produtos", + column: "IdMarca"); + + migrationBuilder.CreateIndex( + name: "IX_Produtos_MarcaId", + table: "Produtos", + column: "MarcaId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Enderecos"); + + migrationBuilder.DropTable( + name: "Produtos"); + + migrationBuilder.DropTable( + name: "Usuarios"); + + migrationBuilder.DropTable( + name: "Marcas"); + } + } +} diff --git a/Prova2_Henrique/Prova/Models/Produto.cs b/Prova2_Henrique/Prova/Models/Produto.cs index 3112d52..4a11c29 100644 --- a/Prova2_Henrique/Prova/Models/Produto.cs +++ b/Prova2_Henrique/Prova/Models/Produto.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; using System.ComponentModel.DataAnnotations.Schema; diff --git a/Prova2_Henrique/Prova/Models/Usuario.cs b/Prova2_Henrique/Prova/Models/Usuario.cs index 38cc702..d15d3e5 100644 --- a/Prova2_Henrique/Prova/Models/Usuario.cs +++ b/Prova2_Henrique/Prova/Models/Usuario.cs @@ -2,7 +2,6 @@ using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; -using Swashbuckle.AspNetCore.Annotations; public class Usuario { diff --git a/Prova2_Henrique/Prova/Program.cs b/Prova2_Henrique/Prova/Program.cs index 257af20..76b8d1b 100644 --- a/Prova2_Henrique/Prova/Program.cs +++ b/Prova2_Henrique/Prova/Program.cs @@ -20,9 +20,9 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddSingleton(); @@ -51,4 +51,4 @@ app.UseAuthorization(); app.MapControllers(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/Prova2_Henrique/Prova/Repositories/MarcaRepository.cs b/Prova2_Henrique/Prova/Repositories/MarcaRepository.cs index e7b491b..f6de0f8 100644 --- a/Prova2_Henrique/Prova/Repositories/MarcaRepository.cs +++ b/Prova2_Henrique/Prova/Repositories/MarcaRepository.cs @@ -1,8 +1,6 @@ using Prova.Data; using Prova.Models; using Microsoft.EntityFrameworkCore; -using System.Collections.Generic; -using System.Threading.Tasks; namespace Prova.Repositories { diff --git a/Prova2_Henrique/Prova/Repositories/ProdutoRepository.cs b/Prova2_Henrique/Prova/Repositories/ProdutoRepository.cs index 96aaed9..dc10bbd 100644 --- a/Prova2_Henrique/Prova/Repositories/ProdutoRepository.cs +++ b/Prova2_Henrique/Prova/Repositories/ProdutoRepository.cs @@ -1,8 +1,6 @@ using Prova.Data; using Prova.Models; using Microsoft.EntityFrameworkCore; -using System.Collections.Generic; -using System.Threading.Tasks; namespace Prova.Repositories { diff --git a/Prova2_Henrique/Prova/Repositories/UsuarioRepository.cs b/Prova2_Henrique/Prova/Repositories/UsuarioRepository.cs index d49ecb9..7ad5927 100644 --- a/Prova2_Henrique/Prova/Repositories/UsuarioRepository.cs +++ b/Prova2_Henrique/Prova/Repositories/UsuarioRepository.cs @@ -1,9 +1,5 @@ using Microsoft.EntityFrameworkCore; using Prova.Data; -using Prova.Models; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace Prova.Repositories { @@ -21,7 +17,7 @@ public async Task> GetAllAsync() return await _context.Usuarios.ToListAsync(); } - public async Task GetByEmailAsync(string email) + public async Task GetPorEmailAssincrono(string email) { return await _context.Usuarios .FirstOrDefaultAsync(u => u.Email == email); diff --git a/Prova2_Henrique/Prova/Services/MarcaService.cs b/Prova2_Henrique/Prova/Services/MarcaService.cs index 890942d..b1a9c2c 100644 --- a/Prova2_Henrique/Prova/Services/MarcaService.cs +++ b/Prova2_Henrique/Prova/Services/MarcaService.cs @@ -1,7 +1,5 @@ using Prova.Repositories; using Prova.Models; -using System.Collections.Generic; -using System.Threading.Tasks; namespace Prova.Services { diff --git a/Prova2_Henrique/Prova/Services/ProdutoService.cs b/Prova2_Henrique/Prova/Services/ProdutoService.cs index 8d8473d..cb16483 100644 --- a/Prova2_Henrique/Prova/Services/ProdutoService.cs +++ b/Prova2_Henrique/Prova/Services/ProdutoService.cs @@ -1,7 +1,5 @@ using Prova.Repositories; using Prova.Models; -using System.Collections.Generic; -using System.Threading.Tasks; namespace Prova.Services { diff --git a/Prova2_Henrique/Prova/Services/UsuarioService.cs b/Prova2_Henrique/Prova/Services/UsuarioService.cs index 49e5258..de7cc24 100644 --- a/Prova2_Henrique/Prova/Services/UsuarioService.cs +++ b/Prova2_Henrique/Prova/Services/UsuarioService.cs @@ -1,7 +1,5 @@ using Prova.Repositories; using Prova.Models; -using System.Collections.Generic; -using System.Threading.Tasks; using Prova.DTOs; using Prova.DTO; @@ -26,7 +24,7 @@ public async Task CriarUsuario(UsuarioCadastroDTO usuarioDto) if (!ValidarCPF(usuarioDto.Cpf)) throw new ArgumentException("CPF inválido."); - var emailExistente = await _usuarioRepository.GetByEmailAsync(usuarioDto.Email); + var emailExistente = await _usuarioRepository.GetPorEmailAssincrono(usuarioDto.Email); if (emailExistente != null) throw new InvalidOperationException("Já existe um usuário com este email."); @@ -63,7 +61,7 @@ public async Task CriarUsuario(UsuarioCadastroDTO usuarioDto) public async Task AtualizarUsuario(string email, UsuarioAtualizacaoDTO usuarioAtualizado) { - var usuarioExistente = await _usuarioRepository.GetByEmailAsync(email); + var usuarioExistente = await _usuarioRepository.GetPorEmailAssincrono(email); if (usuarioExistente == null) throw new InvalidOperationException("Usuário não encontrado."); @@ -83,7 +81,7 @@ public async Task AutenticarUsuario(string email, string senha, string if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(senha)) throw new ArgumentException("Email e senha são obrigatórios."); - var usuario = await _usuarioRepository.GetByEmailAsync(email); + var usuario = await _usuarioRepository.GetPorEmailAssincrono(email); if (usuario == null || usuario.Senha != senha) throw new InvalidOperationException("Email ou senha inválidos.");