Browse Source

add: user controller and API endpoints

Users
KerelOlivier 2 years ago
parent
commit
96271839f6
  1. 56
      Controllers/UserController.cs
  2. 8
      Models/BlogPost.cs
  3. 6
      Models/User.cs
  4. 7
      Program.cs
  5. 4
      appsettings.json

56
Controllers/UserController.cs

@ -1,6 +1,60 @@
using backend.Models;
using backend.Services;
using Microsoft.AspNetCore.Mvc;
namespace backend.Controllers; namespace backend.Controllers;
public class UserController [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();
}
} }

8
Models/BlogPost.cs

@ -7,11 +7,11 @@ public class BlogPost
{ {
[BsonId] [BsonId]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; } public string? Id { get; set; } = null;
public string AuthorId { get; set; } public string AuthorId { get; set; } = null;
public string Title { get; set; } public string Title { get; set; } = null;
public string Content { get; set; } public string Content { get; set; } = null;
//Meta data //Meta data
[BsonRepresentation(BsonType.DateTime)] [BsonRepresentation(BsonType.DateTime)]

6
Models/User.cs

@ -7,9 +7,7 @@ public class User
{ {
[BsonId] [BsonId]
[BsonRepresentation(BsonType.ObjectId)] [BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } public string? Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Email { get; set; } public string Bio { get; set; }
public string Password { get; set; } //Bcrypt encrypted
public int Role { get; set; } //0 = user, 1 = author, 2 = admin
} }

7
Program.cs

@ -1,11 +1,16 @@
using backend.Models; using backend.Models;
using backend.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllers();
builder.Services.Configure<BlogDatabaseSettings>(builder.Configuration.GetSection("BlogDatabase")); builder.Services.Configure<BlogDatabaseSettings>(builder.Configuration.GetSection("BlogDatabase"));
builder.Services.AddSingleton<UserService>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();

4
appsettings.json

@ -2,8 +2,8 @@
"BlogDatabase": { "BlogDatabase": {
"ConnectionString": "mongodb://localhost:27017", "ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "Blog", "DatabaseName": "Blog",
"UserCollectionName": "Users", "UsersCollectionName": "Users",
"PostCollectionName": "Posts" "PostsCollectionName": "Posts"
}, },

Loading…
Cancel
Save