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
|
@ -18,10 +18,13 @@ steps:
|
||||||
environment:
|
environment:
|
||||||
ConnectionString:
|
ConnectionString:
|
||||||
from_secret: ConnectionString
|
from_secret: ConnectionString
|
||||||
|
MySQLConnectionString:
|
||||||
|
from_secret: MySQLConnectionString
|
||||||
commands:
|
commands:
|
||||||
- chmod +x ./build.sh
|
- chmod +x ./build.sh
|
||||||
- ./build.sh
|
- ./build.sh
|
||||||
- sed -i "s/<DatabaseConnectionString>/$ConnectionString/g" output/appsettings.json
|
- sed -i "s/<DatabaseConnectionString>/$ConnectionString/g" output/appsettings.json
|
||||||
|
- sed -i "s/<MySQLConnectionString>/$MySQLConnectionString/g" output/appsettings.json
|
||||||
- cp api.blog.service output/
|
- cp api.blog.service output/
|
||||||
- cp -r ./output/* /output
|
- cp -r ./output/* /output
|
||||||
- name: restart service
|
- name: restart service
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using MongoDB.Driver;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Robware.Api.Blog.Models;
|
using Robware.Api.Blog.Models;
|
||||||
using Robware.Blog;
|
using Robware.Blog;
|
||||||
|
@ -14,10 +16,12 @@ namespace Robware.Api.Blog.Controllers {
|
||||||
|
|
||||||
private readonly ILogger<BlogController> _logger;
|
private readonly ILogger<BlogController> _logger;
|
||||||
private readonly IBlogRepository _blogRepository;
|
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;
|
_logger = logger;
|
||||||
_blogRepository = blogRepository;
|
_blogRepository = blogRepository;
|
||||||
|
_dbProvider = dbProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet(nameof(Get) + "/{url}")]
|
[HttpGet(nameof(Get) + "/{url}")]
|
||||||
|
@ -117,5 +121,16 @@ namespace Robware.Api.Blog.Controllers {
|
||||||
return BadRequest("Tried to publish post that doesn't exist");
|
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.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using MongoDB.Driver;
|
||||||
using Robware.Blog;
|
using Robware.Blog;
|
||||||
using Robware.Data;
|
using Robware.Data.MongoDB;
|
||||||
|
|
||||||
namespace Robware.Api.Blog {
|
namespace Robware.Api.Blog {
|
||||||
public class Startup {
|
public class Startup {
|
||||||
|
@ -19,10 +20,16 @@ namespace Robware.Api.Blog {
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
|
|
||||||
services
|
services
|
||||||
.AddSingleton<IDatabaseProvider>(new MySQLDatabaseProvider(Configuration.GetConnectionString("database")))
|
.AddSingleton<Data.IDatabaseProvider>(new Data.MySQLDatabaseProvider(Configuration.GetConnectionString("mysql")))
|
||||||
|
.AddSingleton<IMongoDatabase>(SetupMongoDatabase())
|
||||||
.AddSingleton<IBlogRepository, BlogRepository>();
|
.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.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
|
||||||
if (env.IsDevelopment()) {
|
if (env.IsDevelopment()) {
|
||||||
|
|
|
@ -7,6 +7,6 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"database": "Server=localhost;User ID=user;Password=pass;Database=db"
|
"database": "mongodb://localhost"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"database": "<DatabaseConnectionString>"
|
"database": "<DatabaseConnectionString>",
|
||||||
|
"mysql": "<MySQLConnectionString>"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue