PhoneToolMX/PhoneToolMX.Models/ViewModels/ExtensionVM.cs

60 lines
1.8 KiB
C#
Raw Normal View History

2023-10-18 04:55:10 +00:00
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,
};
}
2023-10-21 20:46:56 +00:00
// TODO: fix hack
public string NotifyOnChange() => (ExtId == 0 ? Id + 1000 : ExtId).ToString();
2023-10-18 04:55:10 +00:00
}
}