1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-10-05 15:47:20 +02:00

Clean up SettingsController to not check for Null's as empty strings will not be null when returned.

class variables for _settingsSaved, _settingsFailed to easily change all error messages when saving
This commit is contained in:
markus101 2011-02-14 17:20:17 -08:00
parent b2524cf697
commit ff44d05a72
6 changed files with 69 additions and 318 deletions

View File

@ -13,6 +13,7 @@ public class QualityProfile
[Required (ErrorMessage = "A Name is Required")]
[DisplayName("Name")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string Name { get; set; }
public bool UserProfile { get; set; } //Allows us to tell the difference between default and user profiles
@ -22,6 +23,7 @@ public class QualityProfile
[SubSonicIgnore]
[DisplayName("Allowed Qualities String")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string AllowedString { get; set; }
[DisplayName("Cutoff")]

View File

@ -19,6 +19,8 @@ public class SettingsController : Controller
private IIndexerProvider _indexerProvider;
private IQualityProvider _qualityProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private string _settingsSaved = "Settings Saved.";
private string _settingsFailed = "Error Saving Settings, please fix any errors";
public SettingsController(IConfigProvider configProvider, IIndexerProvider indexerProvider, IQualityProvider qualityProvider)
{
@ -146,60 +148,22 @@ public QualityModel GetUpdatedProfileList()
return new QualityModel {DefaultProfileId = defaultQualityProfileId, SelectList = selectList};
}
[HttpPost]
public ActionResult Index(SettingsModel data)
{
try
{
_configProvider.SeriesRoot = data.TvFolder;
}
catch (Exception)
{
Logger.Error("Error saving settings.");
if (Request.IsAjaxRequest())
return Content("Error saving settings.");
return Content("Error saving settings.");
}
if (Request.IsAjaxRequest())
return Content("Settings Saved.");
return Content("Settings Saved.");
}
[HttpPost]
public ActionResult SaveGeneral(SettingsModel data)
{
try
if (ModelState.IsValid)
{
if (data.TvFolder != null)
_configProvider.SeriesRoot = data.TvFolder;
//if (data.Quality != null)
// _configProvider.SetValue("Quality", data.Quality);
}
catch (Exception e)
{
Logger.ErrorException(e.Message, e);
if (Request.IsAjaxRequest())
return Content("Error Saving Settings, please fix any errors");
return Content("Error Saving Settings, please fix any errors");
_configProvider.SeriesRoot = data.TvFolder;
return Content(_settingsSaved);
}
if (Request.IsAjaxRequest())
return Content("Settings Saved.");
return Content("Settings Saved.");
return Content(_settingsFailed);
}
[HttpPost]
public ActionResult SaveIndexers(IndexerSettingsModel data)
{
try
if (ModelState.IsValid)
{
//Todo: Only allow indexers to be enabled if user information has been provided
foreach (var indexer in data.Indexers)
@ -208,38 +172,17 @@ public ActionResult SaveIndexers(IndexerSettingsModel data)
_indexerProvider.Update(indexer);
}
if (data.NzbMatrixUsername != null)
_configProvider.SetValue("NzbMatrixUsername", data.NzbMatrixUsername);
_configProvider.SetValue("NzbMatrixUsername", data.NzbMatrixUsername);
_configProvider.SetValue("NzbMatrixApiKey", data.NzbMatrixApiKey);
_configProvider.SetValue("NzbsOrgUId", data.NzbsOrgUId);
_configProvider.SetValue("NzbsOrgHash", data.NzbsOrgHash);
_configProvider.SetValue("NzbsrusUId", data.NzbsrusUId);
_configProvider.SetValue("NzbsrusHash", data.NzbsrusHash);
if (data.NzbMatrixApiKey != null)
_configProvider.SetValue("NzbMatrixApiKey", data.NzbMatrixApiKey);
if (data.NzbsOrgUId != null)
_configProvider.SetValue("NzbsOrgUId", data.NzbsOrgUId);
if (data.NzbsOrgHash != null)
_configProvider.SetValue("NzbsOrgHash", data.NzbsOrgHash);
if (data.NzbsrusUId != null)
_configProvider.SetValue("NzbsrusUId", data.NzbsrusUId);
if (data.NzbsrusHash != null)
_configProvider.SetValue("NzbsrusHash", data.NzbsrusHash);
}
catch (Exception e)
{
Logger.ErrorException(e.Message, e);
if (Request.IsAjaxRequest())
return Content("Error Saving Settings, please fix any errors");
return Content("Error Saving Settings, please fix any errors");
return Content(_settingsSaved);
}
if (Request.IsAjaxRequest())
return Content("Settings Saved.");
return Content("Settings Saved.");
return Content(_settingsFailed);
}
[HttpPost]
@ -247,118 +190,67 @@ public ActionResult SaveDownloads(DownloadSettingsModel data)
{
if (ModelState.IsValid)
{
try
{
if (data.SyncFrequency > 15)
_configProvider.SetValue("SyncFrequency", data.SyncFrequency.ToString());
_configProvider.SetValue("SyncFrequency", data.SyncFrequency.ToString());
_configProvider.SetValue("DownloadPropers", data.DownloadPropers.ToString());
if (data.Retention > 0)
_configProvider.SetValue("Retention", data.Retention.ToString());
if (data.SabHost != null)
_configProvider.SetValue("SabHost", data.SabHost);
if (data.SabPort > 0)
_configProvider.SetValue("SabPort", data.SabPort.ToString());
if (data.SabApiKey != null)
_configProvider.SetValue("SabApiKey", data.SabApiKey);
if (data.SabUsername != null)
_configProvider.SetValue("SabUsername", data.SabUsername);
if (data.SabPassword != null)
_configProvider.SetValue("SabPassword", data.SabPassword);
if (data.SabCategory != null)
_configProvider.SetValue("SabCategory", data.SabCategory);
_configProvider.SetValue("Retention", data.Retention.ToString());
_configProvider.SetValue("SabHost", data.SabHost);
_configProvider.SetValue("SabPort", data.SabPort.ToString());
_configProvider.SetValue("SabApiKey", data.SabApiKey);
_configProvider.SetValue("SabUsername", data.SabUsername);
_configProvider.SetValue("SabPassword", data.SabPassword);
_configProvider.SetValue("SabCategory", data.SabCategory);
_configProvider.SetValue("SabPriority", data.SabPriority.ToString());
if (Request.IsAjaxRequest())
return Content("Settings Saved.");
return Content("Settings Saved.");
}
catch (Exception e)
{
Logger.ErrorException(e.Message, e);
if (Request.IsAjaxRequest())
return Content("Error Saving Settings, please fix any errors");
return Content("Error Saving Settings, please fix any errors");
}
return Content(_settingsSaved);
}
if (Request.IsAjaxRequest())
return Content("Error Saving Settings, please fix any errors");
return Content("Error Saving Settings, please fix any errors");
return Content(_settingsFailed);
}
[HttpPost]
public ActionResult SaveQuality(QualityModel data)
{
try
if (ModelState.IsValid)
{
if (ModelState.IsValid)
_configProvider.SetValue("DefaultQualityProfile", data.DefaultProfileId.ToString());
//Saves only the Default Quality, skips User Profiles since none exist
if (data.UserProfiles == null)
return Content(_settingsSaved);
foreach (var dbProfile in _qualityProvider.GetAllProfiles().Where(q => q.UserProfile))
{
_configProvider.SetValue("DefaultQualityProfile", data.DefaultProfileId.ToString());
if (!data.UserProfiles.Exists(p => p.ProfileId == dbProfile.ProfileId))
_qualityProvider.Delete(dbProfile.ProfileId);
}
foreach (var dbProfile in _qualityProvider.GetAllProfiles().Where(q => q.UserProfile))
foreach (var profile in data.UserProfiles)
{
Logger.Debug(String.Format("Updating User Profile: {0}", profile));
profile.Allowed = new List<QualityTypes>();
foreach (var quality in profile.AllowedString.Split(','))
{
if (!data.UserProfiles.Exists(p => p.ProfileId == dbProfile.ProfileId))
_qualityProvider.Delete(dbProfile.ProfileId);
var qType = (QualityTypes)Enum.Parse(typeof(QualityTypes), quality);
profile.Allowed.Add(qType);
}
foreach (var profile in data.UserProfiles)
{
Logger.Debug(String.Format("Updating User Profile: {0}", profile));
//If the Cutoff value selected is not in the allowed list then use the last allowed value, this should be validated on submit
if (!profile.Allowed.Contains(profile.Cutoff))
return Content("Error Saving Settings, please fix any errors");
//profile.Cutoff = profile.Allowed.Last();
profile.Allowed = new List<QualityTypes>();
foreach (var quality in profile.AllowedString.Split(','))
{
var qType = (QualityTypes) Enum.Parse(typeof (QualityTypes), quality);
profile.Allowed.Add(qType);
}
if (profile.ProfileId > 0)
_qualityProvider.Update(profile);
//If the Cutoff value selected is not in the allowed list then use the last allowed value, this should be validated on submit
if (!profile.Allowed.Contains(profile.Cutoff))
throw new InvalidOperationException("Invalid Cutoff Value");
//profile.Cutoff = profile.Allowed.Last();
else
_qualityProvider.Add(profile);
if (profile.ProfileId > 0)
_qualityProvider.Update(profile);
else
_qualityProvider.Add(profile);
}
return Content(_settingsSaved);
}
}
catch (Exception e)
{
Logger.ErrorException(e.Message, e);
if (Request.IsAjaxRequest())
return Content("Error Saving Settings, please fix any errors");
return Content("Error Saving Settings, please fix any errors");
}
if (Request.IsAjaxRequest())
return Content("Settings Saved.");
return Content("Settings Saved.");
}
[HttpPost]
public ActionResult SortedList(List<object> items)
{
return Content("Settings Saved.");
return Content(_settingsFailed);
}
}
}

View File

@ -57,6 +57,7 @@ public int SabPort
[DataType(DataType.Text)]
[DisplayName("SABnzbd API Key")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String SabApiKey
{
get;
@ -65,6 +66,7 @@ public String SabApiKey
[DataType(DataType.Text)]
[DisplayName("SABnzbd Username")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String SabUsername
{
get;
@ -73,6 +75,7 @@ public String SabUsername
[DataType(DataType.Text)]
[DisplayName("SABnzbd Password")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String SabPassword
{
get;
@ -80,6 +83,7 @@ public String SabPassword
}
[DataType(DataType.Text)]
[DisplayFormat(ConvertEmptyStringToNull = false)]
[DisplayName("SABnzbd Category")]
public String SabCategory
{
@ -88,7 +92,6 @@ public String SabCategory
}
[Required(ErrorMessage = "Please select a valid priority")]
[DataType(DataType.Text)]
[DisplayName("SABnzbd Priority")]
public SabnzbdPriorityType SabPriority
{

View File

@ -12,6 +12,7 @@ public class IndexerSettingsModel
{
[DataType(DataType.Text)]
[DisplayName("NZBMatrix Username")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String NzbMatrixUsername
{
get;
@ -20,6 +21,7 @@ public String NzbMatrixUsername
[DataType(DataType.Text)]
[DisplayName("NZBMatrix API Key")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String NzbMatrixApiKey
{
get;
@ -28,6 +30,7 @@ public String NzbMatrixApiKey
[DataType(DataType.Text)]
[DisplayName("NZBs.Org UID")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String NzbsOrgUId
{
get;
@ -36,6 +39,7 @@ public String NzbsOrgUId
[DataType(DataType.Text)]
[DisplayName("NZBs.Org Hash")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String NzbsOrgHash
{
get;
@ -44,6 +48,7 @@ public String NzbsOrgHash
[DataType(DataType.Text)]
[DisplayName("NZBsRus UID")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String NzbsrusUId
{
get;
@ -52,6 +57,7 @@ public String NzbsrusUId
[DataType(DataType.Text)]
[DisplayName("NZBsRus Hash")]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public String NzbsrusHash
{
get;

View File

@ -11,8 +11,8 @@ namespace NzbDrone.Web.Models
public class SettingsModel
{
#region General Settings
[DataType(DataType.Text)]
[Required(ErrorMessage = "Please enter a valid TV path")]
[DisplayName("TV Folder")]
public String TvFolder
{
@ -27,159 +27,5 @@ public int Quality
get;
set;
}
#endregion
//#region Indexer Settings
//[DataType(DataType.Text)]
//[DisplayName("NZBMatrix Username")]
//public String NzbMatrixUsername
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("NZBMatrix API Key")]
//public String NzbMatrixApiKey
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("NZBs.Org UID")]
//public String NzbsOrgUId
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("NZBs.Org Hash")]
//public String NzbsOrgHash
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("NZBsRus UID")]
//public String NzbsrusUId
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("NZBsRus Hash")]
//public String NzbsrusHash
//{
// get;
// set;
//}
//public List<Indexer> Indexers
//{
// get;
// set;
//}
//#endregion
//#region Download Settings
////Sync Frequency
////Download Propers?
////Retention
////SAB Host/IP
////SAB Port
////SAB APIKey
////SAB Username
////SAB Password
////SAB Category
////SAB Priority
//[DataType(DataType.Text)]
//[DisplayName("Sync Frequency")]
//public int SyncFrequency
//{
// get;
// set;
//}
//[DisplayName("Download Propers")]
//public bool DownloadPropers
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("Retention")]
//public int Retention
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("SABnzbd Host")]
//public String SabHost
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("SABnzbd Port")]
//public int SabPort
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("SABnzbd API Key")]
//public String SabApiKey
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("SABnzbd Username")]
//public String SabUsername
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("SABnzbd Password")]
//public String SabPassword
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("SABnzbd Category")]
//public String SabCategory
//{
// get;
// set;
//}
//[DataType(DataType.Text)]
//[DisplayName("SABnzbd Priority")]
//public SabnzbdPriorityType SabPriority
//{
// get;
// set;
//}
//#endregion
}
}

View File

@ -10,5 +10,7 @@
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.RenderPartial(ViewData["viewName"].ToString()); %>
</asp:Content>