Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions MiniBlog/Controllers/ArticleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,38 @@ namespace MiniBlog.Controllers
[Route("[controller]")]
public class ArticleController : ControllerBase
{
private readonly ArticleStore articleStore = null!;
private readonly UserStore userStore = null!;
private readonly ArticleService articleService = null!;
private readonly UserService userService = null!;

public ArticleController(ArticleService articleService)
public ArticleController(ArticleStore articleStore, UserStore userStore, UserService userService, ArticleService articleService)
{
//this.articleStore = articleStore;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: remove dead code

//this.userStore = userStore;
this.userService = userService;
this.articleService = articleService;
}

[HttpGet]
public async Task<List<Article>> List()
public async Task<List<Article>> ListAsync()
{
return await articleService.GetAll();
Console.WriteLine(articleService.GetAllAsync());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: remove code for debug

return await articleService.GetAllAsync();
}

[HttpPost]
public async Task<IActionResult> Create(Article article)
public async Task<IActionResult> CreateAsync(Article article)
{
var addedArticle = await articleService.CreateArticle(article);
var createdArticle = await articleService.CreateArticleAsync(article);

return CreatedAtAction(nameof(GetById), new { id = article.Id }, addedArticle);
return CreatedAtAction(nameof(GetById), new { id = article.Id }, article);
}

[HttpGet("{id}")]
public Article? GetById(Guid id)
public async Task<Article> GetById(string id)
{
return articleService.GetById(id);
return await articleService.GetByIdAsync(id);
}
}
}
42 changes: 15 additions & 27 deletions MiniBlog/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MiniBlog.Model;
using MiniBlog.Services;
using MiniBlog.Stores;

namespace MiniBlog.Controllers
Expand All @@ -13,59 +15,45 @@ public class UserController : ControllerBase
{
private readonly ArticleStore articleStore = null!;
private readonly UserStore userStore = null!;
private readonly ArticleService articleService = null!;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: remove unused articleService

private readonly UserService userService = null!;

public UserController(ArticleStore articleStore, UserStore userStore)
public UserController(ArticleStore articleStore, UserService userService, UserStore userStore)
{
this.articleStore = articleStore;
this.userStore = userStore;
this.userService = userService;
}

[HttpPost]
public IActionResult Register(User user)
public async Task<IActionResult> RegisterAsync(User user)
{
if (!userStore.Users.Exists(_ => user.Name.ToLower() == _.Name.ToLower()))
{
userStore.Users.Add(user);
}

return CreatedAtAction(nameof(GetByName), new { name = user.Name }, GetByName(user.Name));
await userService.CreateUserAsync(user);
return CreatedAtAction(nameof(GetByNameAsync), new { name = user.Name }, GetByNameAsync(user.Name));
}

[HttpGet]
public List<User> GetAll()
public async Task<List<User>> GetAllAsync()
{
return userStore.Users;
return await userService.GetAllAsync();
}

[HttpPut]
public User Update(User user)
{
var foundUser = userStore.Users.FirstOrDefault(_ => _.Name == user.Name);
if (foundUser != null)
{
foundUser.Email = user.Email;
}

return foundUser;
return userService.Update(user);
}

[HttpDelete]
public User Delete(string name)
{
var foundUser = userStore.Users.FirstOrDefault(_ => _.Name == name);
if (foundUser != null)
{
userStore.Users.Remove(foundUser);
articleStore.Articles.RemoveAll(a => a.UserName == foundUser.Name);
}

return foundUser;
return userService.Delete(name);
}

[HttpGet("{name}")]
public User GetByName(string name)
public async Task<User> GetByNameAsync(string name)
{
return userStore.Users.FirstOrDefault(_ => _.Name.ToLower() == name.ToLower());
return await userService.GetUser(name);
}
}
}
1 change: 1 addition & 0 deletions MiniBlog/MiniBlog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
Expand Down
13 changes: 6 additions & 7 deletions MiniBlog/Model/Article.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;

namespace MiniBlog.Model
{
Expand All @@ -22,10 +22,9 @@ public Article(string userName, string title, string content)

[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; } = null!;

public string UserName { get; set; } = null!;
public string Title { get; set; } = null!;
public string Content { get; set; } = null!;
public string? Id { get; set; } = null;
public string UserName { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
}
}
4 changes: 4 additions & 0 deletions MiniBlog/Model/User.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using System.Collections.Generic;

namespace MiniBlog.Model
Expand All @@ -14,6 +16,8 @@ public User(string name, string email = "[email protected]")
this.Email = email;
}

public static string CollectionName { get; set; } = "User";

public string Name { get; set; }

public string Email { get; set; }
Expand Down
26 changes: 15 additions & 11 deletions MiniBlog/Repositories/ArticleRepository.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
using System;
using MiniBlog.Model;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using MiniBlog.Model;
using MongoDB.Driver;

namespace MiniBlog.Repositories
{
public class ArticleRepository : IArticleRepository
{
private readonly IMongoCollection<Article> articleCollection;

public ArticleRepository(IMongoClient mongoClient)
{
var mongoDatabase = mongoClient.GetDatabase("MiniBlog");

articleCollection = mongoDatabase.GetCollection<Article>(Article.CollectionName);
IMongoDatabase mongoDataBase = mongoClient.GetDatabase("MiniBlog");
articleCollection = mongoDataBase.GetCollection<Article>(Article.CollectionName);
}

public async Task<List<Article>> GetArticles() =>
await articleCollection.Find(_ => true).ToListAsync();
public async Task<List<Article>> GetAllArticles()
{
return await articleCollection.Find(_ => true).ToListAsync();
}

public async Task<Article> CreateArticle(Article article)
{
await articleCollection.InsertOneAsync(article);
var newArticle = new Article(article.UserName, article.Title, article.Content);
await articleCollection.InsertOneAsync(newArticle);
return await articleCollection.Find(a => a.Title == article.Title).FirstAsync();
}

public async Task<Article> GetArticle(string id)
{
return await articleCollection.Find(a => a.Id == id).FirstOrDefaultAsync();
}
}
}
7 changes: 4 additions & 3 deletions MiniBlog/Repositories/IArticleRepository.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using MiniBlog.Model;

namespace MiniBlog.Repositories
{
public interface IArticleRepository
{
public Task<List<Article>> GetArticles();
public Task<List<Article>> GetAllArticles();
public Task<Article> CreateArticle(Article article);
public Task<Article> GetArticle(string id);
}
}
}
15 changes: 15 additions & 0 deletions MiniBlog/Repositories/IUserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MiniBlog.Model;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MiniBlog.Repositories
{
public interface IUserRepository
{
public Task<List<User>> GetAllUsers();
public Task<User> CreateUser(User user);
public Task<User> GetUser(string name);
public Task<User> Update(User user);
public Task<User> Delete(string name);
}
}
44 changes: 44 additions & 0 deletions MiniBlog/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using MiniBlog.Model;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MiniBlog.Repositories
{
public class UserRepository : IUserRepository

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well: nice to also provide repository for user

{
private readonly IMongoCollection<User> userCollection;
public UserRepository(IMongoClient mongoClient)
{
IMongoDatabase mongoDataBase = mongoClient.GetDatabase("MiniBlog");
userCollection = mongoDataBase.GetCollection<User>(User.CollectionName);
}

public async Task<User> CreateUser(User user)
{
var newUser = new User(user.Name,user.Email);
await userCollection.InsertOneAsync(newUser);
return await userCollection.Find(a => a.Name == user.Name).FirstAsync();
}

public Task<User> Delete(string name)
{
throw new System.NotImplementedException();
}

public async Task<List<User>> GetAllUsers()
{
return await userCollection.Find(_ => true).ToListAsync();
}

public async Task<User> GetUser(string name)
{
return await userCollection.Find(user => user.Name.ToLower() == name.ToLower()).FirstOrDefaultAsync();
}

public Task<User> Update(User user)
{
throw new System.NotImplementedException();
}
}
}
84 changes: 43 additions & 41 deletions MiniBlog/Services/ArticleService.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
using MiniBlog.Model;
using MiniBlog.Repositories;
using MiniBlog.Stores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MiniBlog.Model;
using MiniBlog.Repositories;
using MiniBlog.Stores;

namespace MiniBlog.Services;

public class ArticleService
namespace MiniBlog.Services
{
private readonly ArticleStore articleStore = null!;
private readonly UserStore userStore = null!;
private readonly IArticleRepository articleRepository = null!;

public ArticleService(ArticleStore articleStore, UserStore userStore, IArticleRepository articleRepository)
{
this.articleStore = articleStore;
this.userStore = userStore;
this.articleRepository = articleRepository;
}

public async Task<Article?> CreateArticle(Article article)
{
// if (article.UserName != null)
// {
// if (!userStore.Users.Exists(_ => article.UserName == _.Name))
// {
// userStore.Users.Add(new User(article.UserName));
// }

// articleStore.Articles.Add(article);
// }

// return articleStore.Articles.Find(articleExisted => articleExisted.Title == article.Title);

return await this.articleRepository.CreateArticle(article);
}

public async Task<List<Article>> GetAll()
{
return await articleRepository.GetArticles();
}

public Article? GetById(Guid id)
public class ArticleService
{
return articleStore.Articles.FirstOrDefault(article => article.Id == id.ToString());
private readonly ArticleStore articleStore = null;
private readonly UserStore userStore = null;
private readonly IArticleRepository articleRepository = null;
private readonly IUserRepository userRepository = null;

public ArticleService(ArticleStore articleStore, UserStore userStore, IArticleRepository articleRepository, IUserRepository userRepository)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job: dependency injection

{
this.articleStore = articleStore;
this.userStore = userStore;
this.articleRepository = articleRepository;
this.userRepository = userRepository;
}

public async Task<Article> CreateArticleAsync(Article article)
{
Article createdArticle = null;
if (article.UserName != null)
{
if (await userRepository.GetUser(article.UserName) == null)
{
await userRepository.CreateUser(new User(article.UserName));
}

createdArticle = await articleRepository.CreateArticle(article);
}

return await GetByIdAsync(createdArticle?.Id);
}

public async Task<Article> GetByIdAsync(string id)
{
return await articleRepository.GetArticle(id);
}

public async Task<List<Article>> GetAllAsync()
{
return await articleRepository.GetAllArticles();
}
}
}
Loading