예제 #1
0
        private async Task <Download> PopulateAuthor(Download download)
        {
            if (ValidateQuery(await _userTable.SelectById(download.CreatorId), out User creator))
            {
                download.CreatorId = creator.Id;
                download.Authors   = new List <User>
                {
                    creator
                };
            }
            else
            {
                throw new Exception("Provided creator id could not be found.");
            }

            var downloadAuthors = await _downloadAuthorTable.SelectByDownload(download.Id);

            foreach (DownloadAuthorModel downloadAuthor in downloadAuthors)
            {
                if (ValidateQuery(await _userTable.SelectById(downloadAuthor.UserId), out User user))
                {
                    download.Authors.Add(user);
                }
            }

            return(download);
        }
예제 #2
0
        public async Task <User> GetUserById(string userid)
        {
            if (ValidateQuery(await _userTable.SelectById(userid), out User user)) // Get user with specified id, check if user exists
            {
                user.Roles = new List <Role>();

                List <UserRoleModel> userRoles = await _userRoleTable.SelectByUser(userid); // Get user role relations for the user

                foreach (UserRoleModel userRole in userRoles)
                {
                    if (ValidateQuery(await _roleTable.SelectById(userRole.RoleId), out Role role)) // Get role associated with role id
                    {
                        user.Roles.Add(role);                                                       // Add role to user's list of roles
                    }
                }

                // TODO: repeat for alerts and anything else which needs to be added.

                return(user);
            }
            else
            {
                throw new ArgumentException("Provided user ID could not be found.");
            }
        }
예제 #3
0
        public async Task <Connection> CreateConnection(Connection connection)
        {
            if ((await _userTable.SelectById(connection.User.Id)).Any())                   // Check that provided user exists
            {
                if ((await _applicationTable.SelectById(connection.Application.Id)).Any()) // Check that provided application exists
                {
                    connection.UserId = connection.User.Id;
                    connection.AppId  = connection.Application.Id;

                    connection.AuthorizationCode = GenerateAuthCode();

                    if ((await _connectionTable.SelectByUserAndApplication(connection.User.Id, connection.Application.Id)).Any()) // If a connection between the provided user and application already exists
                    {
                        await _connectionTable.Update(connection);
                    }
                    else
                    {
                        await _connectionTable.Insert(connection);
                    }

                    if (ValidateQuery(await _connectionTable.SelectByUserAndApplication(connection.UserId, connection.AppId), out Connection output))
                    {
                        return(output);
                    }
                    else
                    {
                        throw new Exception("Error occurred getting new connection.");
                    }
                }
                else
                {
                    throw new ArgumentException("Provided application ID could not be found.");
                }
            }
            else
            {
                throw new ArgumentException("Provided user ID could not be found.");
            }
        }
예제 #4
0
        public async Task <Application> GetApplicationById(string appid)
        {
            if (ValidateQuery(await _applicationTable.SelectById(appid), out Application app)) // If the application exists
            {
                if (ValidateQuery(await _userTable.SelectById(app.UserId), out User author))   // If the application's author exists
                {
                    app.Author = author;

                    return(app);
                }
                else
                {
                    throw new Exception("Author of provided application could not be found.");
                }
            }
            else
            {
                return(null);
            }
        }