|
4 | 4 |
|
5 | 5 | from typing import TYPE_CHECKING |
6 | 6 |
|
7 | | -from litestar import Controller, get, post |
| 7 | +from litestar import Controller, get, patch, post |
8 | 8 | from litestar.di import Provide |
9 | 9 | from litestar.params import Dependency, Parameter |
10 | 10 |
|
11 | | -from byte_bot.server.domain import urls |
12 | | -from byte_bot.server.domain.guilds.dependencies import provides_guilds_service |
13 | | -from byte_bot.server.domain.guilds.schemas import GuildSchema |
14 | | -from byte_bot.server.domain.guilds.services import GuildsService # noqa: TC001 |
| 11 | +from byte_bot.server.domain.guilds import urls |
| 12 | +from byte_bot.server.domain.guilds.dependencies import ( |
| 13 | + provides_allowed_users_config_service, |
| 14 | + provides_forum_config_service, |
| 15 | + provides_github_config_service, |
| 16 | + provides_guilds_service, |
| 17 | + provides_sotags_config_service, |
| 18 | +) |
| 19 | +from byte_bot.server.domain.guilds.schemas import ( |
| 20 | + AllowedUsersConfigSchema, |
| 21 | + ForumConfigSchema, |
| 22 | + GitHubConfigSchema, |
| 23 | + GuildSchema, |
| 24 | + SOTagsConfigSchema, |
| 25 | + UpdateableGuildSettingEnum, |
| 26 | +) |
| 27 | +from byte_bot.server.domain.guilds.services import ( |
| 28 | + AllowedUsersConfigService, # noqa: TC001 |
| 29 | + ForumConfigService, # noqa: TC001 |
| 30 | + GitHubConfigService, # noqa: TC001 |
| 31 | + GuildsService, # noqa: TC001 |
| 32 | + SOTagsConfigService, # noqa: TC001 |
| 33 | +) |
15 | 34 |
|
16 | 35 | if TYPE_CHECKING: |
17 | 36 | from advanced_alchemy.filters import FilterTypes |
18 | 37 | from advanced_alchemy.service import OffsetPagination |
19 | 38 |
|
20 | | -__all__ = ("GuildController",) |
| 39 | +__all__ = ("GuildsController",) |
21 | 40 |
|
22 | 41 |
|
23 | | -class GuildController(Controller): |
| 42 | +class GuildsController(Controller): |
24 | 43 | """Controller for guild-based routes.""" |
25 | 44 |
|
26 | 45 | tags = ["Guilds"] |
27 | | - dependencies = {"guilds_service": Provide(provides_guilds_service)} |
| 46 | + dependencies = { |
| 47 | + "guilds_service": Provide(provides_guilds_service), |
| 48 | + "github_service": Provide(provides_github_config_service), |
| 49 | + "sotags_service": Provide(provides_sotags_config_service), |
| 50 | + "allowed_users_service": Provide(provides_allowed_users_config_service), |
| 51 | + "forum_service": Provide(provides_forum_config_service), |
| 52 | + } |
28 | 53 |
|
29 | 54 | @get( |
30 | 55 | operation_id="Guilds", |
@@ -80,3 +105,174 @@ async def create_guild( |
80 | 105 | new_guild = {"guild_id": guild_id, "guild_name": guild_name} |
81 | 106 | await guilds_service.create(new_guild) |
82 | 107 | return f"Guild {guild_name} created." |
| 108 | + |
| 109 | + @patch( |
| 110 | + operation_id="UpdateGuild", |
| 111 | + name="guilds:update", |
| 112 | + summary="Update a guild.", |
| 113 | + path=urls.GUILD_UPDATE, |
| 114 | + ) |
| 115 | + async def update_guild( |
| 116 | + self, |
| 117 | + guilds_service: GuildsService, |
| 118 | + guild_id: int = Parameter( |
| 119 | + title="Guild ID", |
| 120 | + description="The guild ID.", |
| 121 | + ), |
| 122 | + setting: UpdateableGuildSettingEnum = Parameter( |
| 123 | + title="Setting", |
| 124 | + description="The setting to update.", |
| 125 | + ), |
| 126 | + value: str | int = Parameter( |
| 127 | + title="Value", |
| 128 | + description="The new value for the setting.", |
| 129 | + ), |
| 130 | + ) -> GuildSchema | OffsetPagination[GuildSchema]: |
| 131 | + """Update a guild by ID. |
| 132 | +
|
| 133 | + Args: |
| 134 | + guilds_service (GuildsService): Guilds service |
| 135 | + guild_id (Guild.guild_id): Guild ID |
| 136 | + setting (UpdateableGuildSettingEnum): Setting to update |
| 137 | + value (str | int): New value for the setting |
| 138 | +
|
| 139 | + Returns: |
| 140 | + Guild: Updated guild object |
| 141 | + """ |
| 142 | + result = await guilds_service.get(guild_id, id_attribute="guild_id") |
| 143 | + # todo: this is a placeholder, update to grab whichever setting is being update, and update the corresponding |
| 144 | + # tables value based on the setting parameter |
| 145 | + await guilds_service.update({str(setting): value}, item_id=guild_id) |
| 146 | + return guilds_service.to_schema(schema_type=GuildSchema, data=result) |
| 147 | + |
| 148 | + @get( |
| 149 | + operation_id="GuildDetail", |
| 150 | + name="guilds:detail", |
| 151 | + summary="Get guild details.", |
| 152 | + path=urls.GUILD_DETAIL, |
| 153 | + ) |
| 154 | + async def get_guild( |
| 155 | + self, |
| 156 | + guilds_service: GuildsService, |
| 157 | + guild_id: int = Parameter( |
| 158 | + title="Guild ID", |
| 159 | + description="The guild ID.", |
| 160 | + ), |
| 161 | + ) -> GuildSchema: |
| 162 | + """Get a guild by ID. |
| 163 | +
|
| 164 | + Args: |
| 165 | + guilds_service (GuildsService): Guilds service |
| 166 | + guild_id (int): Guild ID |
| 167 | +
|
| 168 | + Returns: |
| 169 | + Guild: Guild object |
| 170 | + """ |
| 171 | + result = await guilds_service.get(guild_id, id_attribute="guild_id") |
| 172 | + return guilds_service.to_schema(schema_type=GuildSchema, data=result) |
| 173 | + |
| 174 | + @get( |
| 175 | + operation_id="GitHubDetail", |
| 176 | + name="guilds:github-config", |
| 177 | + summary="Get GitHub config for a guild.", |
| 178 | + path=urls.GUILD_GITHUB_DETAIL, |
| 179 | + ) |
| 180 | + async def get_guild_github_config( |
| 181 | + self, |
| 182 | + github_service: GitHubConfigService, |
| 183 | + guild_id: int = Parameter( |
| 184 | + title="Guild ID", |
| 185 | + description="The guild ID.", |
| 186 | + ), |
| 187 | + ) -> GitHubConfigSchema | OffsetPagination[GitHubConfigSchema]: |
| 188 | + """Get a guild's GitHub config by ID. |
| 189 | +
|
| 190 | + TODO(#88): a helper method that we can use outside of routes would be nice. |
| 191 | +
|
| 192 | + Args: |
| 193 | + github_service (GitHubConfigService): GitHub config service |
| 194 | + guild_id (int): Guild ID |
| 195 | +
|
| 196 | + Returns: |
| 197 | + GitHubConfig: GitHub config object |
| 198 | + """ |
| 199 | + result = await github_service.get(guild_id, id_attribute="guild_id") |
| 200 | + return github_service.to_schema(schema_type=GitHubConfigSchema, data=result) |
| 201 | + |
| 202 | + @get( |
| 203 | + operation_id="SOTagsDetail", |
| 204 | + name="guilds:sotags-config", |
| 205 | + summary="Get StackOverflow tags config for a guild.", |
| 206 | + path=urls.GUILD_SOTAGS_DETAIL, |
| 207 | + ) |
| 208 | + async def get_guild_sotags_config( |
| 209 | + self, |
| 210 | + sotags_service: SOTagsConfigService, |
| 211 | + guild_id: int = Parameter( |
| 212 | + title="Guild ID", |
| 213 | + description="The guild ID.", |
| 214 | + ), |
| 215 | + ) -> SOTagsConfigSchema | OffsetPagination[SOTagsConfigSchema]: |
| 216 | + """Get a guild's StackOverflow tags config by ID. |
| 217 | +
|
| 218 | + Args: |
| 219 | + sotags_service (SOTagsConfigService): StackOverflow tags config service |
| 220 | + guild_id (int): Guild ID |
| 221 | +
|
| 222 | + Returns: |
| 223 | + SOTagsConfig: StackOverflow tags config object |
| 224 | + """ |
| 225 | + result = await sotags_service.get(guild_id, id_attribute="guild_id") |
| 226 | + return sotags_service.to_schema(schema_type=SOTagsConfigSchema, data=result) |
| 227 | + |
| 228 | + @get( |
| 229 | + operation_id="AllowedUsersDetail", |
| 230 | + name="guilds:allowed-users-config", |
| 231 | + summary="Get allowed users config for a guild.", |
| 232 | + path=urls.GUILD_ALLOWED_USERS_DETAIL, |
| 233 | + ) |
| 234 | + async def get_guild_allowed_users_config( |
| 235 | + self, |
| 236 | + allowed_users_service: AllowedUsersConfigService, |
| 237 | + guild_id: int = Parameter( |
| 238 | + title="Guild ID", |
| 239 | + description="The guild ID.", |
| 240 | + ), |
| 241 | + ) -> AllowedUsersConfigSchema | OffsetPagination[AllowedUsersConfigSchema]: |
| 242 | + """Get a guild's allowed users config by ID. |
| 243 | +
|
| 244 | + Args: |
| 245 | + allowed_users_service (AllowedUsersConfigService): Allowed users config service |
| 246 | + guild_id (int): Guild ID |
| 247 | +
|
| 248 | + Returns: |
| 249 | + AllowedUsersConfig: Allowed users config object |
| 250 | + """ |
| 251 | + result = await allowed_users_service.get(guild_id, id_attribute="guild_id") |
| 252 | + return allowed_users_service.to_schema(schema_type=AllowedUsersConfigSchema, data=result) |
| 253 | + |
| 254 | + @get( |
| 255 | + operation_id="ForumDetail", |
| 256 | + name="guilds:forum-config", |
| 257 | + summary="Get forum config for a guild.", |
| 258 | + path=urls.GUILD_FORUM_DETAIL, |
| 259 | + ) |
| 260 | + async def get_guild_forum_config( |
| 261 | + self, |
| 262 | + forum_service: ForumConfigService, |
| 263 | + guild_id: int = Parameter( |
| 264 | + title="Guild ID", |
| 265 | + description="The guild ID.", |
| 266 | + ), |
| 267 | + ) -> ForumConfigSchema | OffsetPagination[ForumConfigSchema]: |
| 268 | + """Get a guild's forum config by ID. |
| 269 | +
|
| 270 | + Args: |
| 271 | + forum_service (ForumConfigService): Forum config service |
| 272 | + guild_id (int): Guild ID |
| 273 | +
|
| 274 | + Returns: |
| 275 | + ForumConfig: Forum config object |
| 276 | + """ |
| 277 | + result = await forum_service.get(guild_id, id_attribute="guild_id") |
| 278 | + return forum_service.to_schema(schema_type=ForumConfigSchema, data=result) |
0 commit comments