Added: Add Release Status to Metadata Profile, Default to Official Only (#250)
* Added: Add Release Status to Metadata Profile, Default to Official Only * Fixed: Unit Test and Comparisonpull/261/head
parent
9d7c6bc961
commit
df4e1e9b26
@ -0,0 +1,60 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import React, { Component } from 'react';
|
||||
import classNames from 'classnames';
|
||||
import CheckInput from 'Components/Form/CheckInput';
|
||||
import styles from './TypeItem.css';
|
||||
|
||||
class ReleaseStatusItem extends Component {
|
||||
|
||||
//
|
||||
// Listeners
|
||||
|
||||
onAllowedChange = ({ value }) => {
|
||||
const {
|
||||
albumTypeId,
|
||||
onMetadataReleaseStatusItemAllowedChange
|
||||
} = this.props;
|
||||
|
||||
onMetadataReleaseStatusItemAllowedChange(albumTypeId, value);
|
||||
}
|
||||
|
||||
//
|
||||
// Render
|
||||
|
||||
render() {
|
||||
const {
|
||||
name,
|
||||
allowed
|
||||
} = this.props;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classNames(
|
||||
styles.metadataProfileItem
|
||||
)}
|
||||
>
|
||||
<label
|
||||
className={styles.albumTypeName}
|
||||
>
|
||||
<CheckInput
|
||||
containerClassName={styles.checkContainer}
|
||||
name={name}
|
||||
value={allowed}
|
||||
onChange={this.onAllowedChange}
|
||||
/>
|
||||
{name}
|
||||
</label>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
ReleaseStatusItem.propTypes = {
|
||||
albumTypeId: PropTypes.number.isRequired,
|
||||
name: PropTypes.string.isRequired,
|
||||
allowed: PropTypes.bool.isRequired,
|
||||
sortIndex: PropTypes.number.isRequired,
|
||||
onMetadataReleaseStatusItemAllowedChange: PropTypes.func
|
||||
};
|
||||
|
||||
export default ReleaseStatusItem;
|
@ -0,0 +1,87 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import React, { Component } from 'react';
|
||||
import FormGroup from 'Components/Form/FormGroup';
|
||||
import FormLabel from 'Components/Form/FormLabel';
|
||||
import FormInputHelpText from 'Components/Form/FormInputHelpText';
|
||||
import ReleaseStatusItem from './ReleaseStatusItem';
|
||||
import styles from './TypeItems.css';
|
||||
|
||||
class ReleaseStatusItems extends Component {
|
||||
|
||||
//
|
||||
// Render
|
||||
|
||||
render() {
|
||||
const {
|
||||
metadataProfileItems,
|
||||
errors,
|
||||
warnings,
|
||||
...otherProps
|
||||
} = this.props;
|
||||
|
||||
return (
|
||||
<FormGroup>
|
||||
<FormLabel>Release Statuses</FormLabel>
|
||||
<div>
|
||||
|
||||
{
|
||||
errors.map((error, index) => {
|
||||
return (
|
||||
<FormInputHelpText
|
||||
key={index}
|
||||
text={error.message}
|
||||
isError={true}
|
||||
isCheckInput={false}
|
||||
/>
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
{
|
||||
warnings.map((warning, index) => {
|
||||
return (
|
||||
<FormInputHelpText
|
||||
key={index}
|
||||
text={warning.message}
|
||||
isWarning={true}
|
||||
isCheckInput={false}
|
||||
/>
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
<div className={styles.albumTypes}>
|
||||
{
|
||||
metadataProfileItems.map(({ allowed, releaseStatus }, index) => {
|
||||
return (
|
||||
<ReleaseStatusItem
|
||||
key={releaseStatus.id}
|
||||
albumTypeId={releaseStatus.id}
|
||||
name={releaseStatus.name}
|
||||
allowed={allowed}
|
||||
sortIndex={index}
|
||||
{...otherProps}
|
||||
/>
|
||||
);
|
||||
})
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</FormGroup>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
ReleaseStatusItems.propTypes = {
|
||||
metadataProfileItems: PropTypes.arrayOf(PropTypes.object).isRequired,
|
||||
errors: PropTypes.arrayOf(PropTypes.object),
|
||||
warnings: PropTypes.arrayOf(PropTypes.object),
|
||||
formLabel: PropTypes.string
|
||||
};
|
||||
|
||||
ReleaseStatusItems.defaultProps = {
|
||||
errors: [],
|
||||
warnings: []
|
||||
};
|
||||
|
||||
export default ReleaseStatusItems;
|
@ -0,0 +1,63 @@
|
||||
using Marr.Data.Converters;
|
||||
using Marr.Data.Mapping;
|
||||
using Newtonsoft.Json;
|
||||
using NzbDrone.Core.Music;
|
||||
using System;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Converters
|
||||
{
|
||||
public class ReleaseStatusIntConverter : JsonConverter, IConverter
|
||||
{
|
||||
public object FromDB(ConverterContext context)
|
||||
{
|
||||
if (context.DbValue == DBNull.Value)
|
||||
{
|
||||
return ReleaseStatus.Official;
|
||||
}
|
||||
|
||||
var val = Convert.ToInt32(context.DbValue);
|
||||
|
||||
return (ReleaseStatus) val;
|
||||
}
|
||||
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
{
|
||||
return FromDB(new ConverterContext {ColumnMap = map, DbValue = dbValue});
|
||||
}
|
||||
|
||||
public object ToDB(object clrValue)
|
||||
{
|
||||
if (clrValue == DBNull.Value)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (clrValue as ReleaseStatus == null)
|
||||
{
|
||||
throw new InvalidOperationException("Attempted to save a albumtype that isn't really a albumtype");
|
||||
}
|
||||
|
||||
var language = (ReleaseStatus) clrValue;
|
||||
return (int) language;
|
||||
}
|
||||
|
||||
public Type DbType => typeof(int);
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(ReleaseStatus);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
|
||||
JsonSerializer serializer)
|
||||
{
|
||||
var item = reader.Value;
|
||||
return (ReleaseStatus) Convert.ToInt32(item);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(ToDB(value));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using FluentMigrator;
|
||||
using Newtonsoft.Json;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(12)]
|
||||
public class add_release_status : NzbDroneMigrationBase
|
||||
{
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Alter.Table("MetadataProfiles").AddColumn("ReleaseStatuses").AsString().WithDefaultValue("");
|
||||
Execute.WithConnection(ConvertProfile);
|
||||
}
|
||||
|
||||
private void ConvertProfile(IDbConnection conn, IDbTransaction tran)
|
||||
{
|
||||
var updater = new ProfileUpdater11(conn, tran);
|
||||
updater.AddDefaultReleaseStatus();
|
||||
updater.Commit();
|
||||
}
|
||||
}
|
||||
|
||||
public class Profile12
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public List<ProfileItem12> ReleaseStatuses { get; set; }
|
||||
}
|
||||
|
||||
public class ProfileItem12
|
||||
{
|
||||
public int ReleaseStatus { get; set; }
|
||||
public bool Allowed { get; set; }
|
||||
}
|
||||
|
||||
public enum ReleaseStatus12
|
||||
{
|
||||
Official = 0,
|
||||
Promotional = 1,
|
||||
Bootleg = 2,
|
||||
Pseudo = 3
|
||||
}
|
||||
|
||||
public class ProfileUpdater11
|
||||
{
|
||||
private readonly IDbConnection _connection;
|
||||
private readonly IDbTransaction _transaction;
|
||||
|
||||
private List<Profile12> _profiles;
|
||||
|
||||
public ProfileUpdater11(IDbConnection conn, IDbTransaction tran)
|
||||
{
|
||||
_connection = conn;
|
||||
_transaction = tran;
|
||||
|
||||
_profiles = GetProfiles();
|
||||
}
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
foreach (var profile in _profiles)
|
||||
{
|
||||
using (var updateProfileCmd = _connection.CreateCommand())
|
||||
{
|
||||
updateProfileCmd.Transaction = _transaction;
|
||||
updateProfileCmd.CommandText =
|
||||
"UPDATE MetadataProfiles SET ReleaseStatuses = ? WHERE Id = ?";
|
||||
updateProfileCmd.AddParameter(profile.ReleaseStatuses.ToJson());
|
||||
updateProfileCmd.AddParameter(profile.Id);
|
||||
|
||||
updateProfileCmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
_profiles.Clear();
|
||||
}
|
||||
|
||||
public void AddDefaultReleaseStatus()
|
||||
{
|
||||
foreach (var profile in _profiles)
|
||||
{
|
||||
profile.ReleaseStatuses = new List<ProfileItem12>
|
||||
{
|
||||
new ProfileItem12
|
||||
{
|
||||
ReleaseStatus = (int)ReleaseStatus12.Official,
|
||||
Allowed = true
|
||||
},
|
||||
new ProfileItem12
|
||||
{
|
||||
ReleaseStatus = (int)ReleaseStatus12.Promotional,
|
||||
Allowed = false
|
||||
},
|
||||
new ProfileItem12
|
||||
{
|
||||
ReleaseStatus = (int)ReleaseStatus12.Bootleg,
|
||||
Allowed = false
|
||||
},
|
||||
new ProfileItem12
|
||||
{
|
||||
ReleaseStatus = (int)ReleaseStatus12.Pseudo,
|
||||
Allowed = false
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private List<Profile12> GetProfiles()
|
||||
{
|
||||
var profiles = new List<Profile12>();
|
||||
|
||||
using (var getProfilesCmd = _connection.CreateCommand())
|
||||
{
|
||||
getProfilesCmd.Transaction = _transaction;
|
||||
getProfilesCmd.CommandText = @"SELECT Id, Name FROM MetadataProfiles";
|
||||
|
||||
using (var profileReader = getProfilesCmd.ExecuteReader())
|
||||
{
|
||||
while (profileReader.Read())
|
||||
{
|
||||
profiles.Add(new Profile12
|
||||
{
|
||||
Id = profileReader.GetInt32(0),
|
||||
Name = profileReader.GetString(1)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return profiles;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.Music
|
||||
{
|
||||
public class ReleaseStatus : IEmbeddedDocument, IEquatable<ReleaseStatus>
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public ReleaseStatus()
|
||||
{
|
||||
}
|
||||
|
||||
private ReleaseStatus(int id, string name)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Id.GetHashCode();
|
||||
}
|
||||
|
||||
public bool Equals(ReleaseStatus other)
|
||||
{
|
||||
if (ReferenceEquals(null, other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (ReferenceEquals(this, other))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return Id.Equals(other.Id);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return ReferenceEquals(this, obj) || Equals(obj as ReleaseStatus);
|
||||
}
|
||||
|
||||
public static bool operator ==(ReleaseStatus left, ReleaseStatus right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
public static bool operator !=(ReleaseStatus left, ReleaseStatus right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
public static ReleaseStatus Official => new ReleaseStatus(0, "Official");
|
||||
public static ReleaseStatus Promotional => new ReleaseStatus(1, "Promotional");
|
||||
public static ReleaseStatus Bootleg => new ReleaseStatus(2, "Bootleg");
|
||||
public static ReleaseStatus Pseudo => new ReleaseStatus(3, "Pseudo");
|
||||
|
||||
|
||||
public static readonly List<ReleaseStatus> All = new List<ReleaseStatus>
|
||||
{
|
||||
Official,
|
||||
Promotional,
|
||||
Bootleg,
|
||||
Pseudo
|
||||
};
|
||||
|
||||
|
||||
public static ReleaseStatus FindById(int id)
|
||||
{
|
||||
if (id == 0)
|
||||
{
|
||||
return Official;
|
||||
}
|
||||
|
||||
ReleaseStatus albumType = All.FirstOrDefault(v => v.Id == id);
|
||||
|
||||
if (albumType == null)
|
||||
{
|
||||
throw new ArgumentException(@"ID does not match a known album type", nameof(id));
|
||||
}
|
||||
|
||||
return albumType;
|
||||
}
|
||||
|
||||
public static explicit operator ReleaseStatus(int id)
|
||||
{
|
||||
return FindById(id);
|
||||
}
|
||||
|
||||
public static explicit operator int(ReleaseStatus albumType)
|
||||
{
|
||||
return albumType.Id;
|
||||
}
|
||||
|
||||
public static explicit operator ReleaseStatus(string type)
|
||||
{
|
||||
var releaseStatus = All.FirstOrDefault(v => v.Name.Equals(type, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
if (releaseStatus == null)
|
||||
{
|
||||
throw new ArgumentException(@"Status does not match a known release status", nameof(type));
|
||||
}
|
||||
|
||||
return releaseStatus;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
namespace NzbDrone.Core.Profiles.Metadata
|
||||
{
|
||||
public class ProfileReleaseStatusItem : IEmbeddedDocument
|
||||
{
|
||||
public ReleaseStatus ReleaseStatus { get; set; }
|
||||
public bool Allowed { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in new issue