Skip to content
Permalink
Browse files

Fix Import Folder Saving and Properly Refactor Out the Logic

  • Loading branch information...
da3dsoul committed May 13, 2019
1 parent acd9ab8 commit e1e26a9b108236ecae074f972dc8f167d9b0693f
@@ -3615,126 +3615,19 @@ public List<ImportFolder> GetImportFolders()
[HttpPost("Folder")]
public CL_Response<ImportFolder> SaveImportFolder(ImportFolder contract)
{
CL_Response<ImportFolder> response = new CL_Response<ImportFolder>
{
ErrorMessage = string.Empty,
Result = null
};
CL_Response<ImportFolder> folder = new CL_Response<ImportFolder>();
try
{
SVR_ImportFolder ns = null;
if (contract.ImportFolderID > 0)
{
// update
ns = RepoFactory.ImportFolder.GetByID(contract.ImportFolderID);
if (ns == null)
{
response.ErrorMessage = "Could not find Import Folder ID: " +
contract.ImportFolderID.ToString();
return response;
}
}
else
{
// create
ns = new SVR_ImportFolder();
}

if (string.IsNullOrEmpty(contract.ImportFolderName))
{
response.ErrorMessage = "Must specify an Import Folder name";
return response;
}

if (string.IsNullOrEmpty(contract.ImportFolderLocation))
{
response.ErrorMessage = "Must specify an Import Folder location";
return response;
}

if (contract.CloudID == 0) contract.CloudID = null;

if (contract.CloudID == null && !Directory.Exists(contract.ImportFolderLocation))
{
response.ErrorMessage = "Cannot find Import Folder location";
return response;
}

if (contract.ImportFolderID == 0)
{
SVR_ImportFolder nsTemp =
RepoFactory.ImportFolder.GetByImportLocation(contract.ImportFolderLocation);
if (nsTemp != null)
{
response.ErrorMessage = "An entry already exists for the specified Import Folder location";
return response;
}
}

if (contract.IsDropDestination == 1 && contract.IsDropSource == 1)
{
response.ErrorMessage = "A folder cannot be a drop source and a drop destination at the same time";
return response;
}

// check to make sure we don't have multiple drop folders
IReadOnlyList<SVR_ImportFolder> allFolders = RepoFactory.ImportFolder.GetAll();

if (contract.IsDropDestination == 1)
{
foreach (SVR_ImportFolder imf in allFolders)
{
if (contract.CloudID == imf.CloudID && imf.IsDropDestination == 1 &&
(contract.ImportFolderID == 0 || contract.ImportFolderID != imf.ImportFolderID))
{
imf.IsDropDestination = 0;
RepoFactory.ImportFolder.Save(imf);
}
else if (imf.CloudID != contract.CloudID)
{
if (contract.IsDropSource == 1 && (imf.FolderIsDropDestination || imf.FolderIsDropSource))
{
response.ErrorMessage = "A drop folders cannot have different file systems";
return response;
}
if (contract.IsDropDestination == 1 && (imf.FolderIsDropDestination || imf.FolderIsDropSource))
{
response.ErrorMessage = "A drop folders cannot have different file systems";
return response;
}
}
}
}

ns.ImportFolderName = contract.ImportFolderName;
ns.ImportFolderLocation = contract.ImportFolderLocation;
ns.IsDropDestination = contract.IsDropDestination;
ns.IsDropSource = contract.IsDropSource;
ns.IsWatched = contract.IsWatched;
ns.ImportFolderType = contract.ImportFolderType;
ns.CloudID = contract.CloudID;

RepoFactory.ImportFolder.Save(ns);

response.Result = ns;
Utils.MainThreadDispatch(() =>
{
ServerInfo.Instance.RefreshImportFolders();
});
ShokoServer.StopWatchingFiles();
ShokoServer.StartWatchingFiles();

return response;
folder.Result = RepoFactory.ImportFolder.SaveImportFolder(contract);
}
catch (Exception ex)
catch (Exception e)
{
logger.Error(ex, ex.ToString());
response.ErrorMessage = ex.Message;
return response;
logger.Error(e);
folder.ErrorMessage = e.Message;
}
return folder;
}


[HttpDelete("Folder/{importFolderID}")]
public string DeleteImportFolder(int importFolderID)
{
@@ -71,27 +71,18 @@ public ActionResult<Counter> CountFolders()
/// </summary>
/// <returns>APIStatus</returns>
[HttpPost("folder/add")]
public ActionResult AddFolder(ImportFolder folder)
public ActionResult<ImportFolder> AddFolder(ImportFolder folder)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (folder.ImportFolderLocation != string.Empty)
try
{
try
{
CL_Response<ImportFolder> response = new ShokoServiceImplementation().SaveImportFolder(folder);

if (string.IsNullOrEmpty(response.ErrorMessage))
{
return Ok();
}
return new APIMessage(500, response.ErrorMessage);
}
catch
{
return InternalError();
}
var result = RepoFactory.ImportFolder.SaveImportFolder(folder);
return result;
}
catch (Exception e)
{
return InternalError(e.Message);
}
return new APIMessage(400, "Bad Request: The Folder path must not be Empty");
}

/// <summary>
@@ -100,34 +91,26 @@ public ActionResult AddFolder(ImportFolder folder)
/// </summary>
/// <returns>APIStatus</returns>
[HttpPost("folder/edit")]
public ActionResult EditFolder(ImportFolder folder)
public ActionResult<ImportFolder> EditFolder(ImportFolder folder)
{
if (!string.IsNullOrEmpty(folder.ImportFolderLocation) && folder.ImportFolderID != 0)
if (string.IsNullOrEmpty(folder.ImportFolderLocation) || folder.ImportFolderID == 0)
return new APIMessage(StatusCodes.Status400BadRequest,
"ImportFolderLocation and ImportFolderID missing");
try
{
try
{
if (folder.IsDropDestination == 1 && folder.IsDropSource == 1)
{
return new APIMessage(409, "The Folder Can't be both Destination and Source Simultaneously");
}
if (folder.ImportFolderID != 0)
{
CL_Response<ImportFolder> response =
new ShokoServiceImplementation().SaveImportFolder(folder);
if (!string.IsNullOrEmpty(response.ErrorMessage))
{
return new APIMessage(500, response.ErrorMessage);
}
return Ok();
}
return new APIMessage(409, "The Import Folder must have an ID");
}
catch
{
return InternalError();
}
if (folder.IsDropDestination == 1 && folder.IsDropSource == 1)
return new APIMessage(StatusCodes.Status409Conflict,
"The Folder Can't be both Destination and Source Simultaneously");

if (folder.ImportFolderID == 0)
return new APIMessage(StatusCodes.Status409Conflict, "The Import Folder must have an ID");
ImportFolder response = RepoFactory.ImportFolder.SaveImportFolder(folder);
return response;
}
catch (Exception e)
{
return InternalError(e.Message);
}
return new APIMessage(400, "ImportFolderLocation and ImportFolderID missing");
}

/// <summary>
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shoko.Models.Client;
using Shoko.Models.Server;
using Shoko.Server.API.Annotations;
using Shoko.Server.API.v2.Models.core;
using Shoko.Server.Repositories;

namespace Shoko.Server.API.v3
{
@@ -25,30 +27,22 @@ public class ImportFolderController : BaseController
/// Handle /api/folder/add
/// Add Folder to Import Folders repository
/// </summary>
/// <returns>APIStatus</returns>
/// <returns>ImportFolder with generated values like ID</returns>
[HttpPost]
public ActionResult AddFolder(ImportFolder folder)
public ActionResult<ImportFolder> AddFolder(ImportFolder folder)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (folder.ImportFolderLocation != string.Empty)
if (folder.ImportFolderLocation == string.Empty)
return new APIMessage(StatusCodes.Status400BadRequest,
"Bad Request: The Folder path must not be Empty");
try
{
try
{
// TODO Do this correctly without calling APIv1
CL_Response<ImportFolder> response = new ShokoServiceImplementation().SaveImportFolder(folder);

if (string.IsNullOrEmpty(response.ErrorMessage))
{
return APIStatus.OK();
}
return new APIMessage(500, response.ErrorMessage);
}
catch
{
return APIStatus.InternalError();
}
return RepoFactory.ImportFolder.SaveImportFolder(folder);
}
catch (Exception e)
{
return APIStatus.InternalError(e.Message);
}
return new APIMessage(400, "Bad Request: The Folder path must not be Empty");
}

/// <summary>
@@ -59,30 +53,25 @@ public ActionResult AddFolder(ImportFolder folder)
[HttpPatch]
public ActionResult EditFolder(ImportFolder folder)
{
if (!String.IsNullOrEmpty(folder.ImportFolderLocation) && folder.ImportFolderID != 0)
{
try
{
// TODO Do this correctly without calling APIv1
if (folder.IsDropDestination == 1 && folder.IsDropSource == 1)
{
return new APIMessage(409, "The Import Folder can't be both Destination and Source");
}
if (String.IsNullOrEmpty(folder.ImportFolderLocation) || folder.ImportFolderID == 0)
return new APIMessage(400, "ImportFolderLocation and ImportFolderID missing");

if (folder.ImportFolderID == 0) return new APIMessage(409, "The Import Folder must have an ID");
CL_Response<ImportFolder> response =
new ShokoServiceImplementation().SaveImportFolder(folder);
if (folder.IsDropDestination == 1 && folder.IsDropSource == 1)
return new APIMessage(StatusCodes.Status409Conflict,
"The Import Folder can't be both Destination and Source");

if (!string.IsNullOrEmpty(response.ErrorMessage)) return new APIMessage(500, response.ErrorMessage);
if (folder.ImportFolderID == 0)
return new APIMessage(StatusCodes.Status409Conflict, "The Import Folder must have an ID");

return APIStatus.OK();
}
catch
{
return APIStatus.InternalError();
}
try
{
RepoFactory.ImportFolder.SaveImportFolder(folder);
return Ok();
}
catch (Exception e)
{
return APIStatus.InternalError(e.Message);
}
return new APIMessage(400, "ImportFolderLocation and ImportFolderID missing");
}

/// <summary>

0 comments on commit e1e26a9

Please sign in to comment.
You can’t perform that action at this time.