|
| 1 | +package net.discordjug.javabot.systems.color_roles.dao; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import javax.annotation.CheckReturnValue; |
| 8 | + |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import net.discordjug.javabot.systems.color_roles.ColorRole; |
| 11 | +import org.springframework.dao.EmptyResultDataAccessException; |
| 12 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 13 | +import org.springframework.stereotype.Repository; |
| 14 | + |
| 15 | +/** |
| 16 | + * Repository class for the COLOR_ROLES and COLOR_ROLE_MEMBERS tables which are used for role colors. |
| 17 | + */ |
| 18 | +@Repository |
| 19 | +@RequiredArgsConstructor |
| 20 | +public class ColorRoleRepository { |
| 21 | + private final JdbcTemplate jdbcTemplate; |
| 22 | + |
| 23 | + /** |
| 24 | + * Gets all color roles of a guild. |
| 25 | + * @param guildId The ID of the guild |
| 26 | + * @return A {@link List} of all registered role colors in the guild |
| 27 | + */ |
| 28 | + public List<ColorRole> getColorRoles(long guildId) { |
| 29 | + return jdbcTemplate.query("SELECT ROLE_ID, COLOR FROM COLOR_ROLES WHERE GUILD_ID=?", |
| 30 | + (rs, _) -> new ColorRole(guildId, rs.getLong(1), new Color(rs.getInt(2))), |
| 31 | + guildId); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Tries to find a color role given the color. |
| 36 | + * @param guildId The ID of the guild containing the role |
| 37 | + * @param color The color of the role to search for |
| 38 | + * @return The color role if it exists or {@link Optional#empty()} |
| 39 | + */ |
| 40 | + public Optional<ColorRole> getFromColor(long guildId, Color color) { |
| 41 | + try { |
| 42 | + ColorRole result = jdbcTemplate.queryForObject("SELECT ROLE_ID FROM COLOR_ROLES WHERE GUILD_ID=? AND COLOR=?", |
| 43 | + (rs, _) -> new ColorRole(guildId, rs.getLong(1), color), |
| 44 | + guildId, color.getRGB()); |
| 45 | + return Optional.ofNullable(result); |
| 46 | + } catch(EmptyResultDataAccessException _) { |
| 47 | + return Optional.empty(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Gets all color roles of a given member. |
| 53 | + * @param guildId The ID of the guild the roles should be part of |
| 54 | + * @param memberId The ID of the member get the roles of |
| 55 | + * @return All color roles of the given member in the given guild |
| 56 | + */ |
| 57 | + public List<ColorRole> getColorRolesOfMember(long guildId, long memberId) { |
| 58 | + return jdbcTemplate.query(""" |
| 59 | + SELECT cr.ROLE_ID, cr.COLOR |
| 60 | + FROM COLOR_ROLES cr |
| 61 | + INNER JOIN COLOR_ROLE_MEMBERS crm ON cr.ROLE_ID = crm.ROLE_ID |
| 62 | + WHERE cr.GUILD_ID=? AND crm.USER_ID=? |
| 63 | + """, |
| 64 | + (rs, _) -> new ColorRole(guildId, rs.getLong(1), new Color(rs.getInt(2))), |
| 65 | + guildId, memberId); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Registers that a member has a given color role. |
| 70 | + * @param color The color role |
| 71 | + * @param memberId The member the role is being added to |
| 72 | + */ |
| 73 | + public void addColorRole(ColorRole color, long memberId) { |
| 74 | + jdbcTemplate.update("INSERT INTO COLOR_ROLES (ROLE_ID, GUILD_ID, COLOR) VALUES (?,?,?)", |
| 75 | + color.roleId(), color.guildId(), color.color().getRGB()); |
| 76 | + addRoleToMember(color.roleId(), memberId); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Registers that a color role has been added to a member. |
| 81 | + * @param roleId The ID of the role the member has |
| 82 | + * @param memberId The ID of the member to update |
| 83 | + */ |
| 84 | + public void addRoleToMember(long roleId, long memberId) { |
| 85 | + jdbcTemplate.update("INSERT INTO COLOR_ROLE_MEMBERS (ROLE_ID, USER_ID) VALUES (?,?)", roleId, memberId); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Registers that a color role has been added to a member. |
| 90 | + * @param roleId The ID of the role removed from the member |
| 91 | + * @param memberId The ID of the member to update |
| 92 | + * @see #cleanRoleIfNoMembers(long) |
| 93 | + */ |
| 94 | + public void removeRoleFromMember(long roleId, long memberId) { |
| 95 | + jdbcTemplate.update("DELETE FROM COLOR_ROLE_MEMBERS WHERE ROLE_ID=? AND USER_ID=?", roleId, memberId); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Removes a role from the database if it has no members. |
| 100 | + * |
| 101 | + * Callers should make sure to actually delete the role if this method returns {@code true}. |
| 102 | + * @param roleId The ID of the role to possibly remove |
| 103 | + * @return {@code true} if the role is registered as removed due to not having any members, else {@code false} |
| 104 | + */ |
| 105 | + @CheckReturnValue |
| 106 | + public boolean cleanRoleIfNoMembers(long roleId) { |
| 107 | + return jdbcTemplate.update("DELETE FROM COLOR_ROLES WHERE ROLE_ID=? AND (SELECT COUNT(*) FROM COLOR_ROLE_MEMBERS WHERE ROLE_ID=?)=0", roleId, roleId) > 0; |
| 108 | + } |
| 109 | +} |
0 commit comments