-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AdrianBalunan
committed
Apr 19, 2026
1 parent
b5993b9
commit 4d11e3c
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...ehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DAO/FavouritesDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package ntnu.systemutvikling.team6.database.DAO; | ||
|
|
||
| import ntnu.systemutvikling.team6.database.DatabaseConnection; | ||
| import ntnu.systemutvikling.team6.models.Charity; | ||
| import ntnu.systemutvikling.team6.models.user.User; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class FavouritesDAO { | ||
|
|
||
| private final DatabaseConnection connection; | ||
|
|
||
| public FavouritesDAO(DatabaseConnection connection) { | ||
| this.connection = connection; | ||
| } | ||
|
|
||
| public boolean addFavourite(User user, Charity charity) { | ||
| String sql = "INSERT INTO Favourites (UUID_user, UUID_charity) VALUES (?, ?)"; | ||
|
|
||
| try (Connection conn = connection.getMySqlConnection(); | ||
| PreparedStatement ps = conn.prepareStatement(sql)) { | ||
|
|
||
| ps.setString(1, user.getId().toString()); | ||
| ps.setString(2, charity.getUUID().toString()); | ||
| return ps.executeUpdate() > 0; | ||
|
|
||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public boolean removeFavourite(User user, Charity charity) { | ||
| String sql = "DELETE FROM Favourites WHERE UUID_user = ? AND UUID_charity = ?"; | ||
|
|
||
| try (Connection conn = connection.getMySqlConnection(); | ||
| PreparedStatement ps = conn.prepareStatement(sql)) { | ||
|
|
||
| ps.setString(1, user.getId().toString()); | ||
| ps.setString(2, charity.getUUID().toString()); | ||
| return ps.executeUpdate() > 0; | ||
|
|
||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public List<Charity> getFavourites(User user) { | ||
| String sql = """ | ||
| SELECT c.* FROM Charity c | ||
| JOIN Favourites f ON c.UUID_charity = f.UUID_charity | ||
| WHERE f.UUID_user = ? | ||
| """; | ||
|
|
||
| List<Charity> favourites = new ArrayList<>(); | ||
|
|
||
| try (Connection conn = connection.getMySqlConnection(); | ||
| PreparedStatement ps = conn.prepareStatement(sql)) { | ||
|
|
||
| ps.setString(1, user.getId().toString()); | ||
| ResultSet rs = ps.executeQuery(); | ||
|
|
||
| while (rs.next()) { | ||
| favourites.add(new Charity( | ||
|
|
||
| )); | ||
| } | ||
|
|
||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| return favourites; | ||
| } | ||
| } |