|
|
|
using backend.Models;
|
|
|
|
using backend.Services;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace backend.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("blog/api/[controller]")]
|
|
|
|
public class UserController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly UserService _userService;
|
|
|
|
|
|
|
|
public UserController(UserService userService)
|
|
|
|
{
|
|
|
|
_userService = userService;
|
|
|
|
}
|
|
|
|
//API endpoints
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<List<User>> Get() => await _userService.Get();
|
|
|
|
|
|
|
|
[HttpGet("{id:length(24)}")]
|
|
|
|
public async Task<ActionResult<User>> Get(string id)
|
|
|
|
{
|
|
|
|
var user = await _userService.GetAsync(id);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
[HttpPost]
|
|
|
|
public async Task<IActionResult> Post([FromBody] User user)
|
|
|
|
{
|
|
|
|
await _userService.CreateAsync(user);
|
|
|
|
return CreatedAtAction(nameof(Get), new {id = user.Id}, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPut("{id:length(24)}")]
|
|
|
|
public async Task<IActionResult> Put(string id, [FromBody] User user)
|
|
|
|
{
|
|
|
|
var userToUpdate = await _userService.GetAsync(id);
|
|
|
|
if (userToUpdate == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
await _userService.UpdateAsync(id, user);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpDelete("{id:length(24)}")]
|
|
|
|
public async Task<IActionResult> Delete(string id)
|
|
|
|
{
|
|
|
|
var user = await _userService.GetAsync(id);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
await _userService.RemoveAsync(user.Id);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|