PhoneToolMX/PhoneToolMX.Models/ViewModels/ExtensionVM.cs
2023-10-17 21:55:10 -07:00

58 lines
1.7 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,
};
}
}
}