Giter VIP home page Giter VIP logo

apiprodutos's Introduction

ApiProdutos

Criação de API com .NET Core 5.0

Projeto ASP .NET Core Web API

  1. Criar entidade Produto (modelo anêmico)
  2. Criar o contexto AppDbContext (mapeamento, configurações)
  3. Definir a alimentação da tabela Produtos (HasData)
  4. Definir string de conexão com o MySql no arquivo appsettings.json
  5. Configurar o serviço no arquivo Startup (provedor, conexão)
  6. Criar o Contoller ProdutosController via Scaffolding

Visual Studio 2019 (Community)

  • Criar um projeto (C#, Todas as Plataformas, Web)

    1. API Web do ASP.NET Core
  • Criar Pasta "Data"

    1. Adicionar -> Nova Pasta
  • Definir Entidade de Dominio (dentro da pasta Data)

    1. Adicionar -> Novo Item -> Classe "Produto.cs" public int Id { get; set; } public string Nome { get; set; } public decimal Preco { get; set; } public int Estoque { get; set; }
  • Ferramentas -> Gerenciador de Pacotes do NuGet -> Gerenciador Pacotes NuGet para a Solução...

    1. Procurar

    Microsoft.EntityFrameworkCore

    Microsoft.EntityFrameworkCore.Tools

    Microsoft.EntityFrameworkCore.Design

    Pomelo.EntityFrameworkCore.MySql

    Microsoft.AspNetCore.Authentication.JwtBearer

    Microsoft.AspNetCore.Authentication.OpenIdConnect

  • Definir Classe de Contexto (dentro da pasta Data)

    1. Adicionar -> Novo Item -> Classe "AppDbContext.cs" (Mapeamento orm entre a entidade Produto e a tabela Produtos)
    public class AppDbContext : DbContext
    {
      public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
      {
      }
    
      public DbSet<Produto> Produtos { get; set; }
    
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
         modelBuilder.Entity<Produto>()
            .Property(p => p.Nome)
            .HasMaxLength(80);
    
         modelBuilder.Entity<Produto>()
            .Property(p => p.Preco)
            .HasPrecision(10, 2);
    
         modelBuilder.Entity<Produto>()
            .HasData(
               new Produto { Id = 1, Nome = "Caderno", Preco = 7.95M, Estoque = 10},
               new Produto { Id = 2, Nome = "Borracha", Preco = 2.45M, Estoque = 30},
               new Produto { Id = 3, Nome = "Estojo", Preco = 6.25M, Estoque = 15});
      }
    }
    
  • Definir configuração do Serviço no arquivo "Startup.cs"

    1. Incluir em "public void ConfigureServices"
    string mySqlConnection = Configuration.GetConnectionString("DefaultConnection");
    
    services.AddDbContextPool<AppDbContext>(options =>
      options.UseMySql(mySqlConnection,
      ServerVersion.AutoDetect(mySqlConnection)));
    
  • Definir sessão "ConnectionStrings" no arquivo "appsettings.json"

    "ConnectionStrings": {
      "DefaultConnection": "server=localhost; port=3306; database=produtosdb; user=root; password=; Persist Security Info=False;"
    },
    
  • Ferramentas -> Gerenciador de Pacotes do NuGet -> Console do Gerenciador de Pacotes

  1. Gerar o arquivo de Migration

PM> add-migration Inicial

  1. Aplicar alterações no BD

PM> update-database

  • Criar API na Pasta "Controller"

    1. Adicionar -> Controlador -> API -> Controlador MVC com exibições, usando o Entity Framework

    Classe do modelo: "Produto"

    Classe de contexto de dados: "AppDbContext"

    Nome do controlador: ProdutosController

apiprodutos's People

Contributors

aquareladesign avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.