Add items crud to api
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
using agologumApi.Models;
|
||||
using agologumApi.Services;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UsersController : ControllerBase {
|
||||
|
||||
private readonly UserService service_;
|
||||
|
||||
public UsersController(UserService service) {
|
||||
service_ = service;
|
||||
}
|
||||
|
||||
[AllowAnonymous] // accessible if not authorized
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<User>>> getUsers() {
|
||||
return Ok(await service_.GetAll());
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<User>> getUser(int id) {
|
||||
|
||||
var user = await service_.Get(id);
|
||||
|
||||
if (user == null) return NotFound();
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[Authorize] // testing the authorization
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<User>> createUser(User user) {
|
||||
|
||||
var created = await service_.Create(user);
|
||||
|
||||
return CreatedAtAction(
|
||||
nameof(getUser),
|
||||
new { id = created.Id },
|
||||
created
|
||||
);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPut("{id}")]
|
||||
public async Task<ActionResult<User>> updateUser(int id, User user) {
|
||||
|
||||
var updated = await service_.Update(user);
|
||||
|
||||
if (updated == null) return NotFound();
|
||||
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult> deleteUser(int id) {
|
||||
|
||||
var success = await service_.Delete(id);
|
||||
|
||||
if (!success) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user