protected override void OnHandle(AddSiteCommand command) { using (var uow = UowFactory.Create()) { Validate(command, uow); var portal = uow.Context .Where <Entities.Sites.Portal>(p => true) .Single(); var sysAdminRole = uow.Context .Where <Entities.Security.Role>(r => r.RoleType == RoleTypeCodes.Administrator && r.IsSystemRole) .Single(); string ownerUsername = command.OwnerUsername; var ownerUser = uow.Context .Where <Entities.Security.User>(u => u.UserName == ownerUsername) .Single(); var newSite = new Entities.Sites.Site { Name = command.Name, Title = command.SiteTitle, Description = command.Description, Portal = portal, IsDefault = command.IsDefault, IsActive = command.IsActive, IsPrivate = !command.IsPublic, Owner = ownerUser, }; if (!ownerUser.Roles.Any(r => r.RoleType == RoleTypeCodes.Administrator)) { var siteAdminRole = uow.Context .Query <Entities.Security.Role>() .Where(r => r.RoleType == RoleTypeCodes.Administrator && r.IsSystemRole) .SingleOrDefault(); ownerUser.Roles.Add(siteAdminRole); } newSite.Users.Add(ownerUser); uow.Context.Add(newSite); portal.Sites.Add(newSite); uow.Complete(); } }
private void Validate(IPersistenceUnitOfWork uow, Entities.Sites.Site siteToDelete) { long siteId = siteToDelete.Id; bool hasZones = uow.Context .Query <Entities.Sites.Zone>() .Any(s => s.Site.Id == siteId); if (hasZones) { throw new ValidationException("Site has zones, please delete them first and try again."); } bool hasPosts = uow.Context .Query <Entities.Posts.Post>() .Any(e => e.Site.Id == siteId); if (hasPosts) { throw new ValidationException("Site has posts, please delete them first and try again."); } }