Inicio Eventos e Equipas

This commit is contained in:
José Valdiviesso 2019-02-26 01:52:05 +00:00
parent 7fc4063c46
commit df782124de
8 changed files with 124 additions and 24 deletions

View File

@ -28,7 +28,6 @@ namespace api.Controllers
public UserManager<User> _userManager { get; } public UserManager<User> _userManager { get; }
public SignInManager<User> _signInManager { get; } public SignInManager<User> _signInManager { get; }
public IUsersRepository _repo { get; } public IUsersRepository _repo { get; }
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly RoleManager<Role> _roleManager; private readonly RoleManager<Role> _roleManager;

View File

@ -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<IActionResult> GetEvents()
{
var Events = await _repo.GetEvents();
return Ok(Events);
}
// GET api/events/[id]
// GET events id x
[HttpGet("{id}")]
public async Task<IActionResult> GetEvent(int id)
{
var Event = await _repo.GetEvent(id);
return Ok(Event);
}
}
}

View File

@ -34,11 +34,12 @@ namespace api.Controllers
} }
// GET api/teams // GET api/teams
// GET all teams
[HttpGet] [HttpGet]
public async Task<IActionResult> GetTeams() public async Task<IActionResult> GetTeams()
{ {
var values= await context.Values.ToListAsync(); var Teams = await context.Teams.ToArrayAsync();
return Ok(values); return Ok(Teams);
} }
} }
} }

View File

@ -5,8 +5,8 @@ using Microsoft.EntityFrameworkCore;
namespace api.Data namespace api.Data
{ {
public class DataContext : IdentityDbContext<User,Role,int,IdentityUserClaim<int>, public class DataContext : IdentityDbContext<User,Role,int,IdentityUserClaim<int>,UserRole,IdentityUserLogin<int>,
UserRole,IdentityUserLogin<int>,IdentityRoleClaim<int>,IdentityUserToken<int>> IdentityRoleClaim<int>,IdentityUserToken<int>>
{ {
public DataContext(DbContextOptions<DataContext> options):base(options) { } public DataContext(DbContextOptions<DataContext> options):base(options) { }
@ -24,31 +24,31 @@ namespace api.Data
public DbSet<Log>Logs{get;set;} public DbSet<Log>Logs{get;set;}
public DbSet<Product>Products{get;set;} public DbSet<Product>Products{get;set;}
protected override void OnModelCreating(ModelBuilder builder) protected override void OnModelCreating(ModelBuilder builder)
{ {
base.OnModelCreating(builder); base.OnModelCreating(builder);
//para o ef saber as relações //para o ef saber as relações
builder.Entity<UserRole>(userRole => builder.Entity<UserRole>(userRole =>
{ {
userRole.HasKey(ur=> new {ur.UserId, ur.RoleId}); userRole.HasKey(ur=> new {ur.UserId, ur.RoleId});
userRole.HasOne( ur=>ur.Role) userRole.HasOne( ur=>ur.Role)
.WithMany(r=>r.UserRoles) .WithMany(r=>r.UserRoles)
.HasForeignKey(ur=> ur.RoleId) .HasForeignKey(ur=> ur.RoleId)
.IsRequired(); .IsRequired();
userRole.HasOne( ur=>ur.User) userRole.HasOne( ur=>ur.User)
.WithMany(r=>r.UserRoles) .WithMany(r=>r.UserRoles)
.HasForeignKey(ur=> ur.UserId) .HasForeignKey(ur=> ur.UserId)
.IsRequired(); .IsRequired();
}); });
} }
} }
} }

View File

@ -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<Event> GetEvent(int id)
{
var Event = await _context.Events.FirstOrDefaultAsync(e=>e.Id == id);
return Event;
}
public async Task<IEnumerable<Event>> GetEvents()
{
var Events = await _context.Events.ToListAsync();
return Events;
}
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using api.Models;
namespace api.Data
{
public interface IEventsRepository
{
Task<IEnumerable<Event>> GetEvents();
Task<Event> GetEvent(int id);
}
}

View File

@ -45,6 +45,7 @@ namespace api
services.AddAutoMapper(); services.AddAutoMapper();
services.AddScoped<IUsersRepository, UsersRepository>(); services.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<IEventsRepository, EventsRepository>();
//define a connection string indicada em appsettings.json //define a connection string indicada em appsettings.json
services.AddDbContext<DataContext>(x=>x.UseMySql(Configuration.GetConnectionString("DefaultConnection"))); services.AddDbContext<DataContext>(x=>x.UseMySql(Configuration.GetConnectionString("DefaultConnection")));