Advertisement
joaopaulofcc

VideogameController.cs

Nov 24th, 2021
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using projetoProva.Models;
  8.  
  9. namespace projetoProva.Controllers
  10. {
  11.     [Route("[controller]/[action]")]
  12.     [ApiController]
  13.     public class VideogameController : ControllerBase
  14.     {
  15.         private BDContexto contexto;
  16.        
  17.         public VideogameController(BDContexto bdContexto)
  18.         {
  19.             contexto = bdContexto;
  20.         }
  21.        
  22.        
  23.         [HttpGet]
  24.         public List<Videogame> Listar()
  25.         {
  26.             return contexto.Videogame.ToList();
  27.         }
  28.  
  29.  
  30.         [HttpGet]
  31.         public List<Videogame> ListarPorFabricante(string fabricante)
  32.         {
  33.             return contexto.Videogame.Where(c => c.Fabricante == fabricante).ToList();
  34.         }
  35.  
  36.  
  37.         [HttpGet]
  38.         public List<Videogame> Visualizar(int id)
  39.         {
  40.             return contexto.Videogame.Where(v => v.Id == id).ToList();
  41.         }
  42.  
  43.  
  44.         [HttpPost]
  45.         public string Cadastrar([FromBody]Videogame dados)
  46.         {
  47.             contexto.Add(dados);
  48.             contexto.SaveChanges();
  49.            
  50.             return "Videogame cadastrado com sucesso!";
  51.         }
  52.  
  53.  
  54.         [HttpDelete]
  55.         public string Excluir([FromBody]int id)
  56.         {
  57.             try
  58.             {
  59.                 Videogame dados = contexto.Videogame.FirstOrDefault(v => v.Id == id);
  60.  
  61.                 contexto.Remove(dados);
  62.                 contexto.SaveChanges();
  63.        
  64.                 return "Videogame excluído com sucesso!";
  65.             }
  66.             catch (System.Exception ex)
  67.             {
  68.                 return ex.Message;
  69.             }
  70.         }
  71.            
  72.        
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement