The backend api for the blog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5 KiB

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("{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();
}
}