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 Task> Get() => _userService.Get(); [HttpGet("{id:length(24)}")] public async Task> Get(string id) { var user = await _userService.GetAsync(id); if (user == null) { return NotFound(); } return user; } [HttpPost] public async Task Post([FromBody] User user) { await _userService.CreateAsync(user); return CreatedAtAction(nameof(Get), new {id = user.Id}, user); } [HttpPut("{id:length(24)}")] public async Task 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 Delete(string id) { var user = await _userService.GetAsync(id); if (user == null) { return NotFound(); } await _userService.RemoveAsync(user.Id); return NoContent(); } }