Esempio n. 1
0
        public XioResult <ClientModel> GetClient(ClientSpecModel model)
        {
            log.DescribeOperation("GetClient");

            if (model.MicrosoftTenantId != Guid.Empty)
            {
                var existing = db.Clients.SingleOrDefault(x => x.MicrosoftTenantId == model.MicrosoftTenantId);
                if (existing != null)
                {
                    log.LogInformation("GetClient returned existing client.", ("client", existing.UrlPart));
                    return(new XioResult <ClientModel>(true, mapper.Map <ClientModel>(existing)));
                }
            }

            if (!string.IsNullOrWhiteSpace(model.UrlPart))
            {
                var existing = db.Clients.SingleOrDefault(x => x.UrlPart == model.UrlPart);
                if (existing != null)
                {
                    log.LogInformation("GetClient returned existing client.", ("client", existing.UrlPart));
                    return(new XioResult <ClientModel>(true, mapper.Map <ClientModel>(existing)));
                }
            }

            if (model.Id > 0)
            {
                var existing = db.Clients.SingleOrDefault(x => x.Id == model.Id);
                if (existing != null)
                {
                    log.LogInformation("GetClient returned existing client.", ("client", existing.UrlPart));
                    return(new XioResult <ClientModel>(true, mapper.Map <ClientModel>(existing)));
                }
            }

            // this is a create
            var newClient = db.Clients.Add(new Client
            {
                CountryCode       = model.CountryCode,
                PostCode          = model.PostCode,
                ProviderId        = 1, // todo: something with providder
                Title             = model.Title,
                MicrosoftTenantId = model.MicrosoftTenantId,
                UrlPart           = db.GetUrlPart <Client>(model.Title)
            });

            db.SaveChanges();
            log.LogInformation("GetClient created new client.", ("client", newClient.Entity.UrlPart));
            return(new XioResult <ClientModel>(true, mapper.Map <ClientModel>(newClient.Entity)));
        }
Esempio n. 2
0
        public async Task <XioResult <UserModel> > CreateUser(CreateUserModel model)
        {
            var extant = await UserExists(new SingleUserQueryModel { EmailAddress = model.EmailAddress, MicrosoftAccountId = model.MicrosoftAccountId });

            if (extant.IsSuccessful)
            {
                return(new XioResult <UserModel>(false, "User already exists."));
            }

            var client = await db.Clients.SingleOrDefaultAsync(x => x.Id == model.ClientId);

            if (client == null)
            {
                return(new XioResult <UserModel>(false, "Client not found."));
            }


            bool isClientAdmin = false;

            if (!await db.Users.AnyAsync(x => x.ClientId == model.ClientId))
            {
                isClientAdmin = true;
            }

            var newUser = db.Users.Add(new User
            {
                ClientId             = model.ClientId,
                Created              = DateTime.Now,
                EmailAddress         = model.EmailAddress,
                FirstName            = model.FirstName,
                IsClientAdmin        = isClientAdmin,
                JobTitle             = model.JobTitle,
                LastName             = model.LastName,
                LastUsed             = DateTime.Now,
                MicrosoftAccessToken = model.AccessToken,
                MicrosoftAccountId   = model.MicrosoftAccountId,
                PreferredLanguage    = model.PreferredLanguage,
                UrlPart              = db.GetUrlPart <User>($"{model.FirstName} {model.LastName}")
            });

            await db.SaveChangesAsync();

            return(new XioResult <UserModel>(true, mapper.Map <UserModel>(newUser.Entity)));
        }
Esempio n. 3
0
        public ProjectModel Execute(ProjectCreateModel model, ITaskTracker tracker)
        {
            tracker.SetStatusMessage($"Creating project '{model.Title}'");
            var client = db.Clients.SingleOrDefault(x => x.Id == model.ClientId);

            var np = new Project
            {
                UrlPart  = db.GetUrlPart <Project>(model.Title),
                ClientId = model.ClientId,
                Title    = model.Title
            };

            db.Projects.Add(np);

            db.SaveChanges();

            tracker.SetStatusMessage("Beginning task...");
            tracker.SetProgress(0, 3);
            Thread.Sleep(6000);
            tracker.LogInformation("Beginning thing to do.");
            tracker.SetProgress(1);
            Thread.Sleep(8000);
            tracker.LogWarning("Couldn't do this thing but continuing anyway.");
            tracker.SetProgress(2);
            Thread.Sleep(12000);
            tracker.SetProgress(3);
            Thread.Sleep(5000);
            tracker.Complete(true);

            return(tracker.StoreResult(new ProjectModel
            {
                UrlPart = np.UrlPart,
                Title = np.Title,
                Client = mapper.Map <ClientModel>(client)
            }));
        }