public MoldSettingX(MoldSetting ms) : base()
 {
     this.Offset   = ms.Offset;
     base.RawData  = ms.RawData;
     this.Created  = ms.Created;
     this.Modified = ms.Modified;
 }
        public async Task <IActionResult> AddMoldSettingAsync(int moldId, int offset, ushort value, int variable, CancellationToken ct)
        {
            if (moldId <= 0)
            {
                return(NotFound());
            }
            if (offset < 0)
            {
                return(NotFound());
            }

            var orgId = HttpContext.GetOrg();

            using (var db = new ConfigDB()) {
                var mold = await db.Molds.AsNoTracking().Include(m => m.MoldSettings)
                           .Where(m => m.Controller.OrgId.Equals(orgId, StringComparison.OrdinalIgnoreCase))
                           .SingleOrDefaultAsync(m => m.ID == moldId, ct)
                           .ConfigureAwait(false);

                if (mold == null)
                {
                    return(NotFound());
                }

                var mx = await db.MoldSettings.SingleOrDefaultAsync(s => s.MoldId == moldId && s.Offset == offset, ct).ConfigureAwait(false);

                if (mx != null)
                {
                    return(BadRequest($"Mold/Offset [{moldId}/{offset}] already exists."));
                }

                var ms = new MoldSetting()
                {
                    MoldId   = moldId,
                    Offset   = (short)offset,
                    Created  = DateTime.Now,
                    RawData  = value,
                    Variable = variable
                };

                db.MoldSettings.Add(ms);
                await db.SaveChangesAsync(ct).ConfigureAwait(false);

                return(Created($"molds/{ms.MoldId}/settings", new MoldSettingX(ms)));
            }
        }