Use MongoDB for database. Add small migrate function from MySQL
This commit is contained in:
parent
313d668bfe
commit
8b240fb077
5 changed files with 31 additions and 5 deletions
|
@ -1,7 +1,9 @@
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MongoDB.Driver;
|
||||
using Newtonsoft.Json;
|
||||
using Robware.Api.Blog.Models;
|
||||
using Robware.Blog;
|
||||
|
@ -14,10 +16,12 @@ namespace Robware.Api.Blog.Controllers {
|
|||
|
||||
private readonly ILogger<BlogController> _logger;
|
||||
private readonly IBlogRepository _blogRepository;
|
||||
private readonly IDatabaseProvider _dbProvider;
|
||||
|
||||
public BlogController(ILogger<BlogController> logger, IBlogRepository blogRepository) {
|
||||
public BlogController(ILogger<BlogController> logger, IBlogRepository blogRepository, IDatabaseProvider dbProvider) {
|
||||
_logger = logger;
|
||||
_blogRepository = blogRepository;
|
||||
_dbProvider = dbProvider;
|
||||
}
|
||||
|
||||
[HttpGet(nameof(Get) + "/{url}")]
|
||||
|
@ -117,5 +121,16 @@ namespace Robware.Api.Blog.Controllers {
|
|||
return BadRequest("Tried to publish post that doesn't exist");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet(nameof(Migrate))]
|
||||
public async Task<ActionResult> Migrate() {
|
||||
var mysqlRepo = new Data.BlogRepository(_dbProvider);
|
||||
|
||||
var existingPosts = await mysqlRepo.GetAllPostsAsync();
|
||||
foreach (var post in existingPosts)
|
||||
await _blogRepository.SavePost(post);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,9 @@ using Microsoft.AspNetCore.Hosting;
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MongoDB.Driver;
|
||||
using Robware.Blog;
|
||||
using Robware.Data;
|
||||
using Robware.Data.MongoDB;
|
||||
|
||||
namespace Robware.Api.Blog {
|
||||
public class Startup {
|
||||
|
@ -19,10 +20,16 @@ namespace Robware.Api.Blog {
|
|||
services.AddControllers();
|
||||
|
||||
services
|
||||
.AddSingleton<IDatabaseProvider>(new MySQLDatabaseProvider(Configuration.GetConnectionString("database")))
|
||||
.AddSingleton<Data.IDatabaseProvider>(new Data.MySQLDatabaseProvider(Configuration.GetConnectionString("mysql")))
|
||||
.AddSingleton<IMongoDatabase>(SetupMongoDatabase())
|
||||
.AddSingleton<IBlogRepository, BlogRepository>();
|
||||
}
|
||||
|
||||
private IMongoDatabase SetupMongoDatabase() {
|
||||
var client = new MongoClient(Configuration.GetConnectionString("database"));
|
||||
return client.GetDatabase("robware");
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
|
||||
if (env.IsDevelopment()) {
|
||||
|
|
|
@ -7,6 +7,6 @@
|
|||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"database": "Server=localhost;User ID=user;Password=pass;Database=db"
|
||||
"database": "mongodb://localhost"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"database": "<DatabaseConnectionString>"
|
||||
"database": "<DatabaseConnectionString>",
|
||||
"mysql": "<MySQLConnectionString>"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue