-
Notifications
You must be signed in to change notification settings - Fork 4
Yuhua's PR #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Yuhua's PR #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| //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()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| } | ||
| 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 | ||
|
|
@@ -13,59 +15,45 @@ public class UserController : ControllerBase | |
| { | ||
| private readonly ArticleStore articleStore = null!; | ||
| private readonly UserStore userStore = null!; | ||
| private readonly ArticleService articleService = null!; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| } | ||
| 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 | ||
|
|
@@ -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; } | ||
|
|
||
| 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(); | ||
| } | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| } | ||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: remove dead code