57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using OnProfNext.Server.Data;
|
|
using OnProfNext.Shared.Models;
|
|
|
|
namespace OnProfNext.Server.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class OrderUsersController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public OrderUsersController(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
//GET: api/orderusers/{orderId}
|
|
[HttpGet("{orderId}")]
|
|
public async Task<ActionResult<IEnumerable<int>>> GetUsersForOrder(int orderId)
|
|
{
|
|
var userIds = await _context.OrderUsers.Where(ou => ou.OrderId == orderId).Select(ou => ou.UserId).ToListAsync();
|
|
|
|
return Ok(userIds);
|
|
}
|
|
|
|
//POST: api/orderusers
|
|
[HttpPost]
|
|
public async Task<IActionResult> SetUsersForOrder([FromBody] OrderUserAssignmentDto dto)
|
|
{
|
|
var existing = await _context.OrderUsers
|
|
.Where(ou => ou.OrderId == dto.OrderId)
|
|
.ToListAsync();
|
|
|
|
_context.OrderUsers.RemoveRange(existing);
|
|
|
|
var newRelations = dto.UserIds.Select(uid => new OrderUser
|
|
{
|
|
OrderId = dto.OrderId,
|
|
UserId = uid
|
|
});
|
|
|
|
_context.OrderUsers.AddRange(newRelations);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
public class OrderUserAssignmentDto
|
|
{
|
|
public int OrderId { get; set; }
|
|
public List<int> UserIds { get; set; } = new();
|
|
}
|
|
}
|
|
}
|