Added migration code and cleaned up the DB

pull/35/head
tidusjar 8 years ago
parent ce416ae046
commit b62b7c1305

@ -32,7 +32,7 @@ namespace PlexRequests.Core
{
public interface IRequestService
{
long AddRequest(int providerId, RequestedModel model);
long AddRequest(RequestedModel model);
bool CheckRequest(int providerId);
void DeleteRequest(RequestedModel request);
bool UpdateRequest(RequestedModel model);

@ -42,7 +42,7 @@ namespace PlexRequests.Core
Repo = repo;
}
private IRequestRepository Repo { get; }
public long AddRequest(int providerId, RequestedModel model)
public long AddRequest(RequestedModel model)
{
var entity = new RequestBlobs { Type = model.Type, Content = ReturnBytes(model), ProviderId = model.ProviderId };
var id = Repo.Insert(entity);

@ -40,7 +40,7 @@ namespace PlexRequests.Core
private IRepository<RequestedModel> Repo { get; set; }
public long AddRequest(int providerId, RequestedModel model)
public long AddRequest(RequestedModel model)
{
return Repo.Insert(model);
}

@ -24,6 +24,9 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Linq;
using Mono.Data.Sqlite;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
@ -46,6 +49,7 @@ namespace PlexRequests.Core
CreateDefaultSettingsPage();
}
MigrateDb();
return Db.DbConnection().ConnectionString;
}
@ -63,5 +67,28 @@ namespace PlexRequests.Core
var s = new SettingsServiceV2<PlexRequestSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
s.SaveSettings(defaultSettings);
}
private void MigrateDb()
{
var repo = new GenericRepository<RequestedModel>(Db);
var records = repo.GetAll();
var requestedModels = records as RequestedModel[] ?? records.ToArray();
if(!requestedModels.Any())
{ return; }
var jsonRepo = new JsonRequestService(new RequestJsonRepository(Db, new MemoryCacheProvider()));
var result = new List<long>();
foreach (var r in requestedModels)
{
var id = jsonRepo.AddRequest(r);
result.Add(id);
}
if (result.Any(x => x == -1))
{
throw new SqliteException("Could not migrate the DB!");
}
}
}
}

@ -68,7 +68,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repository\SettingsJsonRepository.cs" />
<Compile Include="Repository\RequestJsonRepository.cs" />
<Compile Include="SettingsModel.cs" />
<Compile Include="GenericRepository.cs" />
<Compile Include="RequestedModel.cs" />
<Compile Include="UserRepository.cs" />

@ -1,38 +0,0 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SettingsModel.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Dapper.Contrib.Extensions;
namespace PlexRequests.Store
{
[Table("Settings")]
public class SettingsModel : Entity
{
public int Port { get; set; }
public bool UserAuthentication { get; set; }
public string PlexAuthToken { get; set; }
}
}

@ -8,36 +8,6 @@ CREATE TABLE IF NOT EXISTS User
Password varchar(100) NOT NULL
);
CREATE TABLE IF NOT EXISTS Settings
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Port INTEGER NOT NULL,
UserAuthentication INTEGER NOT NULL,
PlexAuthToken varchar(50)
);
CREATE TABLE IF NOT EXISTS Requested
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Type INTEGER NOT NULL,
ProviderId INTEGER NOT NULL,
ImdbId varchar(50),
Overview varchar(50),
Title varchar(50) NOT NULL,
PosterPath varchar(50) NOT NULL,
ReleaseDate varchar(50) NOT NULL,
Status varchar(50) NOT NULL,
AdminNote varchar(50),
Approved INTEGER NOT NULL,
LatestTv INTEGER NOT NULL,
RequestedBy varchar(50),
RequestedDate varchar(50) NOT NULL,
Available INTEGER(50),
Issues INTEGER,
OtherMessage varchar(50)
);
CREATE TABLE IF NOT EXISTS GlobalSettings
(

@ -225,7 +225,7 @@ namespace PlexRequests.UI.Modules
{
model.Approved = true;
Log.Debug("Adding movie to database requests (No approval required)");
RequestService.AddRequest(movieId, model);
RequestService.AddRequest(model);
return Response.AsJson(new JsonResponseModel { Result = true });
}
@ -235,7 +235,7 @@ namespace PlexRequests.UI.Modules
try
{
Log.Debug("Adding movie to database requests");
var id = RequestService.AddRequest(movieId, model);
var id = RequestService.AddRequest(model);
NotificationService.Publish(model.Title, model.RequestedBy);
@ -307,14 +307,14 @@ namespace PlexRequests.UI.Modules
{
model.Approved = true;
Log.Debug("Adding tv to database requests (No approval required)");
RequestService.AddRequest(showId, model);
RequestService.AddRequest(model);
return Response.AsJson(new JsonResponseModel { Result = true });
}
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." });
}
RequestService.AddRequest(showId, model);
RequestService.AddRequest(model);
NotificationService.Publish(model.Title, model.RequestedBy);
return Response.AsJson(new { Result = true });

Loading…
Cancel
Save