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 SignInManager<User> _signInManager { get; }
public IUsersRepository _repo { get; }
private readonly IMapper _mapper;
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 all teams
[HttpGet]
public async Task<IActionResult> GetTeams()
{
var values= await context.Values.ToListAsync();
return Ok(values);
var Teams = await context.Teams.ToArrayAsync();
return Ok(Teams);
}
}
}

View File

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

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.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<IEventsRepository, EventsRepository>();
//define a connection string indicada em appsettings.json
services.AddDbContext<DataContext>(x=>x.UseMySql(Configuration.GetConnectionString("DefaultConnection")));