PhoneToolMX/PhoneToolMX.Models/ViewModels/ExtensionVM.cs
2023-10-21 13:46:56 -07:00

60 lines
1.8 KiB
C#

using PhoneToolMX.Data;
using System.ComponentModel.DataAnnotations;
namespace PhoneToolMX.Models.ViewModels
{
public class ExtensionVM: IViewModel
{
public int? Id { get; set; }
[Header("Ext.", Primary = true, Small = true)]
public int ExtId { get; set; }
[Header("Directory Name")]
[Required]
[MaxLength(15)]
public string DirectoryName { get; set; }
[Header("Listed?", Small = true)]
[Required]
public bool Listed { get; set; }
public int? HoldMusic { get; set; }
public IOwnedModel ToEntity(PTMXContext ctx)
{
return new Extension
{
Id = Id,
ExtId = ExtId,
DirectoryName = DirectoryName,
Listed = Listed,
HoldMusic = HoldMusic == null ? null : ctx.CustomData.FirstOrDefault(c => c.Id == HoldMusic),
Owners = new List<User>(),
};
}
public IOwnedModel ToEntity(PTMXContext ctx, IOwnedModel current)
{
var ent = ToEntity(ctx);
ent.Owners = current.Owners;
return ent;
}
public IViewModel FromEntity(IOwnedModel entity)
{
if (entity is not Extension extEnt) throw new ArgumentException("entity must be of type Extension");
return new ExtensionVM
{
Id = extEnt.Id,
ExtId = extEnt.ExtId,
DirectoryName = extEnt.DirectoryName,
Listed = extEnt.Listed,
HoldMusic = extEnt.HoldMusic?.Id,
};
}
// TODO: fix hack
public string NotifyOnChange() => (ExtId == 0 ? Id + 1000 : ExtId).ToString();
}
}