mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 08:46:39 +00:00
116 lines
3.2 KiB
C#
116 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TicketManager.Data;
|
|
using TicketManager.Models;
|
|
|
|
namespace TicketManager.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public UsersController(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Users
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
|
|
{
|
|
return await _context.Users
|
|
.Include(p => p.Assignments)
|
|
.Include(p => p.Edits)
|
|
.ToListAsync();
|
|
}
|
|
|
|
// GET: api/Users/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<User>> GetUser(Guid id)
|
|
{
|
|
var user = await _context.Users
|
|
.Include(p => p.Assignments)
|
|
.Include(p => p.Edits)
|
|
.FirstOrDefaultAsync(p => p.Id == id);
|
|
|
|
if (user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
// PUT: api/Users/5
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
// more details see https://aka.ms/RazorPagesCRUD.
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutUser(Guid id, User user)
|
|
{
|
|
if (id != user.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(user).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!UserExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Users
|
|
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
|
// more details see https://aka.ms/RazorPagesCRUD.
|
|
[HttpPost]
|
|
public async Task<ActionResult<User>> PostUser(User user)
|
|
{
|
|
_context.Users.Add(user);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetUser", new { id = user.Id }, user);
|
|
}
|
|
|
|
// DELETE: api/Users/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<User>> DeleteUser(int id)
|
|
{
|
|
var user = await _context.Users.FindAsync(id);
|
|
if (user == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Users.Remove(user);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return user;
|
|
}
|
|
|
|
private bool UserExists(Guid id)
|
|
{
|
|
return _context.Users.Any(e => e.Id == id);
|
|
}
|
|
}
|
|
}
|