From df782124de9ad768eaa44e208c01d67b0dcf05d2 Mon Sep 17 00:00:00 2001 From: ZMiguel Alves Date: Tue, 26 Feb 2019 01:52:05 +0000 Subject: [PATCH] Inicio Eventos e Equipas --- api/Controllers/AuthController.cs | 1 - api/Controllers/EventsController.cs | 50 +++++++++++++++++++ api/Controllers/TeamsController.cs | 5 +- api/Data/DataContext.cs | 42 ++++++++-------- api/Data/EventsRepository.cs | 34 +++++++++++++ api/Data/IEventsRepository.cs | 15 ++++++ api/Startup.cs | 1 + api/obj/Debug/netcoreapp2.1/api.assets.cache | Bin 133820 -> 133820 bytes 8 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 api/Controllers/EventsController.cs create mode 100644 api/Data/EventsRepository.cs create mode 100644 api/Data/IEventsRepository.cs diff --git a/api/Controllers/AuthController.cs b/api/Controllers/AuthController.cs index 527fb2b2..94f95a04 100755 --- a/api/Controllers/AuthController.cs +++ b/api/Controllers/AuthController.cs @@ -28,7 +28,6 @@ namespace api.Controllers public UserManager _userManager { get; } public SignInManager _signInManager { get; } public IUsersRepository _repo { get; } - private readonly IMapper _mapper; private readonly RoleManager _roleManager; diff --git a/api/Controllers/EventsController.cs b/api/Controllers/EventsController.cs new file mode 100644 index 00000000..7383d0db --- /dev/null +++ b/api/Controllers/EventsController.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using api.Data; +using api.Dtos; +using api.Models; +using AutoMapper; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace api.Controllers +{ + + [Authorize] + [Route("api/[controller]")] + [ApiController] + public class EventsController : ControllerBase + { + private readonly DataContext context; + private readonly IEventsRepository _repo; + private readonly IMapper _mapper; + public EventsController(DataContext context,IEventsRepository repo, IMapper mapper) + { + this.context = context; + _mapper = mapper; + _repo = repo; + } + + // GET api/events + // GET all events + [HttpGet] + public async Task GetEvents() + { + var Events = await _repo.GetEvents(); + return Ok(Events); + } + + // GET api/events/[id] + // GET events id x + [HttpGet("{id}")] + public async Task GetEvent(int id) + { + var Event = await _repo.GetEvent(id); + return Ok(Event); + } + } +} diff --git a/api/Controllers/TeamsController.cs b/api/Controllers/TeamsController.cs index e5598d92..6ed8fb1e 100644 --- a/api/Controllers/TeamsController.cs +++ b/api/Controllers/TeamsController.cs @@ -34,11 +34,12 @@ namespace api.Controllers } // GET api/teams + // GET all teams [HttpGet] public async Task GetTeams() { - var values= await context.Values.ToListAsync(); - return Ok(values); + var Teams = await context.Teams.ToArrayAsync(); + return Ok(Teams); } } } diff --git a/api/Data/DataContext.cs b/api/Data/DataContext.cs index 2b4d4713..e6ac56e1 100755 --- a/api/Data/DataContext.cs +++ b/api/Data/DataContext.cs @@ -5,8 +5,8 @@ using Microsoft.EntityFrameworkCore; namespace api.Data { - public class DataContext : IdentityDbContext, - UserRole,IdentityUserLogin,IdentityRoleClaim,IdentityUserToken> + public class DataContext : IdentityDbContext,UserRole,IdentityUserLogin, + IdentityRoleClaim,IdentityUserToken> { public DataContext(DbContextOptions options):base(options) { } @@ -24,31 +24,31 @@ namespace api.Data public DbSetLogs{get;set;} -public DbSetProducts{get;set;} + public DbSetProducts{get;set;} - protected override void OnModelCreating(ModelBuilder builder) - { - base.OnModelCreating(builder); + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); - //para o ef saber as relações - builder.Entity(userRole => - { - userRole.HasKey(ur=> new {ur.UserId, ur.RoleId}); + //para o ef saber as relações + builder.Entity(userRole => + { + userRole.HasKey(ur=> new {ur.UserId, ur.RoleId}); - userRole.HasOne( ur=>ur.Role) - .WithMany(r=>r.UserRoles) - .HasForeignKey(ur=> ur.RoleId) - .IsRequired(); + userRole.HasOne( ur=>ur.Role) + .WithMany(r=>r.UserRoles) + .HasForeignKey(ur=> ur.RoleId) + .IsRequired(); - userRole.HasOne( ur=>ur.User) - .WithMany(r=>r.UserRoles) - .HasForeignKey(ur=> ur.UserId) - .IsRequired(); - - }); - } + userRole.HasOne( ur=>ur.User) + .WithMany(r=>r.UserRoles) + .HasForeignKey(ur=> ur.UserId) + .IsRequired(); + + }); + } } } \ No newline at end of file diff --git a/api/Data/EventsRepository.cs b/api/Data/EventsRepository.cs new file mode 100644 index 00000000..a7fb28d8 --- /dev/null +++ b/api/Data/EventsRepository.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading.Tasks; +using api.Models; +using api.Data; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; + +namespace api.Data +{ + public class EventsRepository : IEventsRepository + { + public EventsRepository(DataContext context) + { + _context = context; + } + + public DataContext _context { get; } + + public async Task GetEvent(int id) + { + var Event = await _context.Events.FirstOrDefaultAsync(e=>e.Id == id); + + return Event; + } + + public async Task> GetEvents() + { + var Events = await _context.Events.ToListAsync(); + + return Events; + } + + } +} \ No newline at end of file diff --git a/api/Data/IEventsRepository.cs b/api/Data/IEventsRepository.cs new file mode 100644 index 00000000..79233e7f --- /dev/null +++ b/api/Data/IEventsRepository.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using api.Models; + +namespace api.Data +{ + public interface IEventsRepository + { + + Task> GetEvents(); + Task GetEvent(int id); + + + } +} \ No newline at end of file diff --git a/api/Startup.cs b/api/Startup.cs index 62aee96b..913485cf 100755 --- a/api/Startup.cs +++ b/api/Startup.cs @@ -45,6 +45,7 @@ namespace api services.AddAutoMapper(); services.AddScoped(); + services.AddScoped(); //define a connection string indicada em appsettings.json services.AddDbContext(x=>x.UseMySql(Configuration.GetConnectionString("DefaultConnection"))); diff --git a/api/obj/Debug/netcoreapp2.1/api.assets.cache b/api/obj/Debug/netcoreapp2.1/api.assets.cache index 5e8a8b3185bcbdacd1934bcab5c58d0f3ec13a5e..e445080c4a5d97600d8c6fc8c69d2dc856b3c451 100755 GIT binary patch delta 71 zcmV-N0J#6Wl?c3*2rW=cM?nGr000~;PWI~S0f}RK*r#b2Tx|*xG&gSbJ5NEK3+%rF dk^?l_0RRM-@JInWlMpKefoz3r0kv!bPzsH)7=i!* delta 71 zcmV-N0J#6Wl?c3*2rW=cM?nGr004EO3y`Zq5y}_NkoM1$<|x@vN!etcoE2jI|C7b% dX7V)J0RRM-@JInWlMpKefoz3r0kv!bPzpAJ9hm?C