Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Backend-test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions Controllers/PessoaController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Collections.Generic;
using Backend_test.Models;
using Backend_test.Repositorio;
using Microsoft.AspNetCore.Mvc;

namespace Backend_test.Controllers
{
[Route("api/[Controller]")]
public class PessoaController : Controller
{
private readonly IPessoaRepositorio _pessoaRep;

public PessoaController(IPessoaRepositorio pessoaRep)
{
_pessoaRep = pessoaRep;
}
[HttpGet]
public IEnumerable<Pessoa> GetAll()
{
return _pessoaRep.GetAll();
}
[HttpGet("{id}", Name="GetPessoa")]
public IActionResult GetById(long id)
{
var pes = _pessoaRep.Find(id);
if(pes==null)
return NotFound();
return new ObjectResult(pes);
}
[HttpPost]
public IActionResult Create([FromBody] Pessoa pessoa)
{
if(pessoa==null)
return BadRequest();
_pessoaRep.Add(pessoa);

return CreatedAtRoute("GetPessoa", new {id=pessoa.Id}, pessoa);
}
[HttpPut("{id}")]
public IActionResult Update(long id, [FromBody] Pessoa pessoa)
{
if(pessoa==null || pessoa.Id != id)
return BadRequest();
var pes = _pessoaRep.Find(id);
if(pes==null)
return NotFound();
pes.Nome = pessoa.Nome;
pes.Telefone = pessoa.Telefone;
pes.Endereco = pessoa.Endereco;
pes.Email = pessoa.Email;
pes.Cpf = pessoa.Cpf;

_pessoaRep.Update(pes);
return new NoContentResult();
}
[HttpDelete("{id}")]
public IActionResult Delete(long id)
{
var pes = _pessoaRep.Find(id);
if(pes==null)
return NotFound();
_pessoaRep.Remove(id);
return new NoContentResult();
}
}
}
45 changes: 45 additions & 0 deletions Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace Backend_test.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
11 changes: 11 additions & 0 deletions Models/BancoContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;

namespace Backend_test.Models
{
public class BancoContext : DbContext
{
public BancoContext(DbContextOptions<BancoContext> options) : base(options){}

public DbSet<Pessoa> Pessoas{get; set;}
}
}
12 changes: 12 additions & 0 deletions Models/Pessoa.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Backend_test.Models
{
public class Pessoa
{
public int Id { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Cpf { get; set; }
public string Telefone { get; set; }
public string Endereco { get; set; }
}
}
24 changes: 24 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Backend_test
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
30 changes: 30 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:9925",
"sslPort": 44373
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Backend_test": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
15 changes: 15 additions & 0 deletions Repositorio/IPessoaRepositorio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using Backend_test.Models;

namespace Backend_test.Repositorio
{
public interface IPessoaRepositorio
{
void Add(Pessoa pessoa);
IEnumerable<Pessoa> GetAll();
Pessoa Find(long id);
void Remove(long id);
void Update(Pessoa pessoa);

}
}
43 changes: 43 additions & 0 deletions Repositorio/PessoaRepositorio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using Backend_test.Models;

namespace Backend_test.Repositorio
{
public class PessoaRepositorio : IPessoaRepositorio
{
private readonly BancoContext _db;
public PessoaRepositorio(BancoContext ctx)
{
_db = ctx;
}
public void Add(Pessoa pessoa)
{
_db.Pessoas.Add(pessoa);
_db.SaveChanges();
}

public Pessoa Find(long id)
{
return _db.Pessoas.FirstOrDefault(u => u.Id == id);
}

public IEnumerable<Pessoa> GetAll()
{
return _db.Pessoas.ToList();
}

public void Remove(long id)
{
var pes = _db.Pessoas.First(u => u.Id == id);
_db.Pessoas.Remove(pes);
_db.SaveChanges();
}

public void Update(Pessoa pessoa)
{
_db.Pessoas.Update(pessoa);
_db.SaveChanges();
}
}
}
53 changes: 53 additions & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Backend_test.Models;
using Backend_test.Repositorio;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Backend_test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BancoContext>(Options =>
Options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<IPessoaRepositorio,PessoaRepositorio>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseMvc();
}
}
}
9 changes: 9 additions & 0 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
11 changes: 11 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"ConnectionString" :{
"DefaultConnection" : "Data Source=\\DESKTOP-QJ0DN1N\\SQLEXPRESS;database=db_positivo;user=sa;password=mar15l82;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
Loading