Example #1
0
 internal static Template CreateFromDataObject(Data.Template template)
 {
     return(new Template
     {
         Id = template.Id,
         Name = template.Name,
         CreatedByUserId = template.CreatedByUserId,
         CreatedDate = template.CreatedDate,
         LastModifiedDate = template.LastModifiedDate,
         VisualProperties = template.VisualProperties
     });
 }
Example #2
0
        public async Task <Template> Link(TemplateLink newlink)
        {
            var workspace = await _workspaceStore.Load(newlink.WorkspaceId);

            if (workspace == null || !workspace.CanEdit(User))
            {
                throw new InvalidOperationException();
            }

            var entity = await _templateStore.Load(newlink.TemplateId);

            if (entity == null || entity.Parent != null || !entity.IsPublished)
            {
                throw new InvalidOperationException();
            }

            if (!User.IsCreator &&
                await _templateStore.AtTemplateLimit(newlink.WorkspaceId))
            {
                throw new TemplateLimitReachedException();
            }

            var newTemplate = new Data.Template
            {
                ParentId    = entity.Id,
                WorkspaceId = newlink.WorkspaceId,
                Name        = $"{entity.Name}-{new Random().Next(100, 999).ToString()}",
                Description = entity.Description,
                Iso         = entity.Iso,
                Networks    = entity.Networks,
                Guestinfo   = entity.Guestinfo
            };

            await _templateStore.Add(newTemplate);

            //TODO: streamline object graph hydration
            newTemplate = await _templateStore.Load(newTemplate.Id);

            return(Mapper.Map <Template>(newTemplate, WithActor()));
        }
Example #3
0
        public async Task <Template> Link(TemplateLink newlink, bool sudo)
        {
            var entity = await _store.Retrieve(newlink.TemplateId);

            if (entity.IsPublished.Equals(false))
            {
                throw new TemplateNotPublished();
            }

            if (!sudo && await _store.AtTemplateLimit(newlink.WorkspaceId))
            {
                throw new TemplateLimitReached();
            }

            var workspace = await _store.DbContext.Workspaces
                            .FirstOrDefaultAsync(w => w.Id == newlink.WorkspaceId)
            ;

            string name = entity.Name.Length > 60
                ? entity.Name.Substring(0, 60)
                : entity.Name
            ;

            var newTemplate = new Data.Template
            {
                ParentId    = entity.Id,
                WorkspaceId = workspace.Id,
                Name        = $"{name}-{new Random().Next(100, 999).ToString()}",
                Description = entity.Description,
                Iso         = entity.Iso,
                Networks    = entity.Networks,
                Guestinfo   = entity.Guestinfo
            };

            await _store.Create(newTemplate);

            return(Mapper.Map <Template>(
                       await _store.Load(newTemplate.Id)
                       ));
        }