-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bf2988d
Showing
53 changed files
with
972 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace DotNet_Introducao_API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[Controller]")] | ||
public class UsuarioController:ControllerBase | ||
{ | ||
[HttpGet("ObterDataHoraAtual")] | ||
public IActionResult ObterDataHora() | ||
{ | ||
|
||
var obj = new { | ||
Data = DateTime.Now.ToLongDateString(), | ||
Hora = DateTime.Now.ToShortTimeString() | ||
}; | ||
|
||
return Ok(obj); | ||
} | ||
|
||
[HttpGet("Apresentar/{nome}")] | ||
public IActionResult Apresentar(string nome){ | ||
var mensagem = $"Olá {nome} seja bem vindo"; //criando mensagem | ||
return Ok(new {mensagem}); // retornando um objeto anônimo | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace DotNet_Introducao_API.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:50037", | ||
"sslPort": 44338 | ||
} | ||
}, | ||
"profiles": { | ||
"DotNet_Introducao_API": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7141;http://localhost:5243", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace DotNet_Introducao_API; | ||
|
||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# O que é uma api? | ||
* Uma api (application programming interface) é uma forma de comunicação dentre computadores ou programas de computadores | ||
* Em outras palavras, é um software que fornece informações para outro software | ||
* <a href="https://dev.to/hackthisfall/what-is-api-explained-in-easy-way-5aih">Ir para explicacao</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# API de feriados | ||
|
||
## Exemplo prático | ||
* Nager.Date: Mostra os feriados dos países | ||
* <a href="https://date.nager.at/">Nager.Date</a> | ||
* Fotos de cachorros | ||
* <a href="https://dog.ceo/dog-api/">Dogs</a> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Documentação e retornos | ||
* Toda api tem uma documentação | ||
* Quando não é encontrado um dado `404 not found` | ||
* Quando o que você passa um parâmetro errado na url `400` é inválido | ||
* Deve obedecer as regras do desenvolvedor da api, request da maneira certa | ||
* Response: Passando a request certa você terá uma responsta certa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Exemplo de uso | ||
## API de entregas | ||
|
||
* E-comerce entrega | ||
* A empresa de transporte depende da informação da empresa de venda | ||
* Uma api pode intermediar essa comunicação | ||
* Empresa de venda informa o produto e o endereço, empresa de entrega informa o status da entrega | ||
|
||
## API interna | ||
|
||
* Front-end (site) | ||
* Back-end (api) | ||
* Banco de dados (sql,nosql) | ||
* Uma api pode ser usada para se comunicar com banco de dados |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Dog api | ||
* A maior coleção de imagem de cachorros | ||
* Toda api tem que ter uma documentação | ||
* Documentação via swagger | ||
* Não precisa ter um texto gigante, pode ser mais objetivo | ||
* Cada api tem um retorno diferente, a documentação deve ensinar como fazer o uso | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Criando nossa api | ||
* executar comando no terminal | ||
|
||
```console | ||
dotnet new webapi | ||
``` | ||
* para rodar a api | ||
* ao usar `watch` não vai precisar recompilar a api após uma mudança. | ||
* ao executar o comando abrirá o swagger | ||
```console | ||
dotnet watch run | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# O que é uma controller | ||
* Classe para agrupar requisições http | ||
* Agrupamento de classes em comum | ||
* Criando um usuario controller | ||
* Primerio criar uma classe chamada `UsuarioController` | ||
* É bom colocar `Controller` no final para identifacar que a classe é um controller | ||
|
||
```csharp | ||
|
||
using Microsoft.AspNetCore.Mvc; // fazer using | ||
namespace DotNet_Introducao_API.Controllers | ||
{ | ||
|
||
[ApiController] // após criar a classe colocar os atrubitos | ||
[Route("[Controller]")] // atributo da rota | ||
public class UsuarioController:ControllerBase //acrescentar ControllerBase | ||
{ | ||
|
||
} | ||
} | ||
|
||
``` | ||
* criando um método get | ||
```csharp | ||
|
||
[ApiController] | ||
[Route("[Controller]")] | ||
public class UsuarioController:ControllerBase | ||
{ | ||
[HttpGet("ObterDataHoraAtual")] //atributo para o método get | ||
public IActionResult ObterDataHora() | ||
{ | ||
|
||
//criando um objeto para retornar data e hora | ||
var obj = new { | ||
Data = DateTime.Now.ToLongDateString(), | ||
Hora = DateTime.Now.ToShortTimeString() | ||
}; | ||
|
||
return Ok(obj); //retorando os valores | ||
} | ||
} | ||
|
||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Etendendo controllers | ||
* `UsuarioController` no swagger apresentará apenas `Usuario` | ||
* Ao nomear da forma adequada a rota será `..\Usuario` pois será omitido controller | ||
* `Get` será o atributo http | ||
* Primeiro a controler e depois o nome do método `..\Usuario\ObterDataHoraAtual` | ||
* com esse caminho é possível realizar as requisições | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Endpoint com parâmetro | ||
```csharp | ||
|
||
[HttpGet("Apresentar/{nome}")] | ||
public IActionResult Apresentar(string nome){ | ||
var mensagem = $"Olá {nome} seja bem vindo"; //criando mensagem | ||
return Ok(new {mensagem}); // retornando um objeto anônimo | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
Oops, something went wrong.