Compare commits
No commits in common. '96271839f66deb25106416d94b2c322ed6ddf397' and 'da51ce88d344cdea18e547f5e5175dfd67937813' have entirely different histories.
96271839f6
...
da51ce88d3
7 changed files with 8 additions and 129 deletions
@ -1,60 +0,0 @@
@@ -1,60 +0,0 @@
|
||||
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(); |
||||
} |
||||
|
||||
|
||||
} |
@ -1,9 +0,0 @@
@@ -1,9 +0,0 @@
|
||||
namespace backend.Models; |
||||
|
||||
public class BlogDatabaseSettings |
||||
{ |
||||
public string ConnectionString { get; set; } = null; |
||||
public string DatabaseName { get; set; } = null; |
||||
public string UsersCollectionName{ get; set; } = null; |
||||
public string PostsCollectionName { get; set; } = null; |
||||
} |
@ -1,38 +0,0 @@
@@ -1,38 +0,0 @@
|
||||
using backend.Models; |
||||
using Microsoft.Extensions.Options; |
||||
using MongoDB.Driver; |
||||
|
||||
namespace backend.Services; |
||||
|
||||
public class UserService |
||||
{ |
||||
private readonly IMongoCollection<User> _users; |
||||
|
||||
public UserService(IOptions<BlogDatabaseSettings> settings) |
||||
{ |
||||
var client = new MongoClient(settings.Value.ConnectionString); |
||||
var database = client.GetDatabase(settings.Value.DatabaseName); |
||||
_users = database.GetCollection<User>(settings.Value.UsersCollectionName); |
||||
} |
||||
|
||||
public async Task<User?> GetAsync(string id) |
||||
{ |
||||
var user = await _users.Find(user => user.Id == id).FirstOrDefaultAsync(); |
||||
return user; |
||||
} |
||||
|
||||
public async Task CreateAsync(User user) |
||||
{ |
||||
await _users.InsertOneAsync(user); |
||||
} |
||||
|
||||
public async Task RemoveAsync(string id) |
||||
{ |
||||
await _users.DeleteOneAsync(user => user.Id == id); |
||||
} |
||||
|
||||
public async Task UpdateAsync(string id, User userIn) |
||||
{ |
||||
await _users.ReplaceOneAsync(user => user.Id == id, userIn); |
||||
} |
||||
} |
Loading…
Reference in new issue