using System.Linq; using Comal.Classes; using InABox.Core; namespace Comal.Stores { public class StockWarehouseStore : BaseStore { protected override void BeforeSave(StockWarehouse entity) { base.BeforeSave(entity); // Try to make sure there is one and one only default warehouse var otherdefaults = Provider.Query( new Filter(x => x.ID).IsNotEqualTo(entity.ID).And(x => x.Default).IsEqualTo(true), new Columns(x => x.ID, x => x.Default) ).Rows.Select(x => x.ToObject()).ToArray(); if (!entity.Default) if (!otherdefaults.Any()) entity.Default = true; if (entity.Default) { entity.Active = true; foreach (var otherdefault in otherdefaults) otherdefault.Default = false; Provider.Save(otherdefaults); } } } }