-
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
Kirill Koval
committed
Nov 22, 2022
1 parent
b0511f3
commit 8f3ed5a
Showing
37 changed files
with
837 additions
and
147 deletions.
There are no files selected for viewing
41 changes: 38 additions & 3 deletions
41
src/main/java/com/kerrrusha/playlistassistant/controller/IndexServlet.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 |
---|---|---|
@@ -1,17 +1,52 @@ | ||
package com.kerrrusha.playlistassistant.controller; | ||
|
||
import com.kerrrusha.playlistassistant.dao.DBException; | ||
import com.kerrrusha.playlistassistant.model.User; | ||
import com.kerrrusha.playlistassistant.model.itunes.ItunesTrack; | ||
import com.kerrrusha.playlistassistant.service.PlaylistProvider; | ||
import com.kerrrusha.playlistassistant.viewmodel.SuitableTracksViewModel; | ||
import org.apache.log4j.Logger; | ||
|
||
import java.io.*; | ||
import java.util.Collection; | ||
|
||
import javax.servlet.RequestDispatcher; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
public class IndexServlet extends HttpServlet { | ||
|
||
private static final Logger logger = Logger.getLogger(IndexServlet.class); | ||
|
||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { | ||
RequestDispatcher rd = request.getRequestDispatcher("index.jsp"); | ||
rd.forward(request, response); | ||
|
||
HttpSession session = request.getSession(false); | ||
boolean loggedIn = (session != null && session.getAttribute("user") != null); | ||
|
||
if (loggedIn) { | ||
try { | ||
setSuitableTracksViewModel(session); | ||
} catch (DBException e) { | ||
logger.error(e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
request.getRequestDispatcher("index.jsp").forward(request, response); | ||
} | ||
|
||
private void setSuitableTracksViewModel(HttpSession session) throws DBException { | ||
final User user = (User) session.getAttribute("user"); | ||
final SuitableTracksViewModel viewModel = new SuitableTracksViewModel(); | ||
final Collection<ItunesTrack> tracksFound; | ||
|
||
tracksFound = new PlaylistProvider().getUserPlaylist(user); | ||
logger.info("Found " + tracksFound.size() + " suitable track for user.id = " + user.getId()); | ||
|
||
viewModel.setTracks(tracksFound); | ||
|
||
session.setAttribute("model", viewModel); | ||
} | ||
} |
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
13 changes: 2 additions & 11 deletions
13
src/main/java/com/kerrrusha/playlistassistant/controller/auth/LoginServlet.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 |
---|---|---|
@@ -1,29 +1,20 @@ | ||
package com.kerrrusha.playlistassistant.controller.auth; | ||
|
||
import com.google.gson.Gson; | ||
import com.kerrrusha.playlistassistant.service.auth.AuthService; | ||
import com.kerrrusha.playlistassistant.service.auth.AuthResultSender; | ||
import com.kerrrusha.playlistassistant.service.auth.LoginService; | ||
import com.kerrrusha.playlistassistant.service.auth.result.AuthResult; | ||
|
||
import javax.servlet.http.*; | ||
import java.io.IOException; | ||
|
||
import static com.kerrrusha.playlistassistant.util.ServletUtil.setJsonToResponse; | ||
|
||
public class LoginServlet extends HttpServlet { | ||
|
||
private final Gson gson = new Gson(); | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
final String login = request.getParameter("login"); | ||
final String password = request.getParameter("password"); | ||
|
||
AuthResult result = new LoginService(login, password).processLogin(); | ||
new AuthService(request).authenticate(result.getUser()); | ||
|
||
setJsonToResponse(response, gson.toJson(result)); | ||
|
||
response.getWriter().flush(); | ||
AuthResultSender.valueOf(result).sendResponse(request, response); | ||
} | ||
} |
13 changes: 2 additions & 11 deletions
13
src/main/java/com/kerrrusha/playlistassistant/controller/auth/RegisterServlet.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 |
---|---|---|
@@ -1,30 +1,21 @@ | ||
package com.kerrrusha.playlistassistant.controller.auth; | ||
|
||
import com.google.gson.Gson; | ||
import com.kerrrusha.playlistassistant.service.auth.AuthService; | ||
import com.kerrrusha.playlistassistant.service.auth.AuthResultSender; | ||
import com.kerrrusha.playlistassistant.service.auth.RegisterService; | ||
import com.kerrrusha.playlistassistant.service.auth.result.AuthResult; | ||
|
||
import javax.servlet.http.*; | ||
import java.io.IOException; | ||
|
||
import static com.kerrrusha.playlistassistant.util.ServletUtil.setJsonToResponse; | ||
|
||
public class RegisterServlet extends HttpServlet { | ||
|
||
private final Gson gson = new Gson(); | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
final String login = request.getParameter("login"); | ||
final String password = request.getParameter("password"); | ||
final String passwordRepeat = request.getParameter("passwordRepeat"); | ||
|
||
AuthResult result = new RegisterService(login, password, passwordRepeat).processRegister(); | ||
new AuthService(request).authenticate(result.getUser()); | ||
|
||
setJsonToResponse(response, gson.toJson(result)); | ||
|
||
response.getWriter().flush(); | ||
AuthResultSender.valueOf(result).sendResponse(request, response); | ||
} | ||
} |
36 changes: 27 additions & 9 deletions
36
...main/java/com/kerrrusha/playlistassistant/controller/user/SetFavouriteArtistsServlet.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 |
---|---|---|
@@ -1,26 +1,44 @@ | ||
package com.kerrrusha.playlistassistant.controller.user; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import com.kerrrusha.playlistassistant.dao.DBException; | ||
import com.kerrrusha.playlistassistant.model.User; | ||
import com.kerrrusha.playlistassistant.model.itunes.ItunesTrack; | ||
import com.kerrrusha.playlistassistant.model.presentable.PresentableArtist; | ||
import com.kerrrusha.playlistassistant.service.PlaylistAssistantService; | ||
import com.kerrrusha.playlistassistant.service.SetFavouriteArtistsService; | ||
import com.kerrrusha.playlistassistant.service.SetSuitableTracksService; | ||
import com.kerrrusha.playlistassistant.util.SetFavouriteArtistsUtil; | ||
import org.apache.log4j.Logger; | ||
|
||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.Collection; | ||
|
||
import static java.util.stream.Collectors.toSet; | ||
|
||
public class SetFavouriteArtistsServlet extends HttpServlet { | ||
|
||
private static final Logger logger = Logger.getLogger(SetFavouriteArtistsServlet.class); | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) { | ||
final Collection<String> selectedArtists = JsonParser.parseString(request.getParameter("selected-artists")) | ||
.getAsJsonArray().asList().stream() | ||
.map(JsonElement::toString) | ||
.collect(toSet()); | ||
logger.info(selectedArtists); | ||
final User user = (User)request.getSession().getAttribute("user"); | ||
final Collection<PresentableArtist> selectedArtists = SetFavouriteArtistsUtil. | ||
mapJsonToPresentableArtists(request.getParameter("selected-artists")); | ||
|
||
try { | ||
new SetFavouriteArtistsService(selectedArtists).setToUser(user); | ||
} catch (DBException e) { | ||
logger.error(e); | ||
throw new RuntimeException(e); | ||
} | ||
|
||
final Collection<ItunesTrack> suitableTracks = new PlaylistAssistantService().generatePlaylist(selectedArtists); | ||
logger.info("Generated " + suitableTracks.size() + " tracks for user.id = " + user.getId()); | ||
try { | ||
new SetSuitableTracksService(suitableTracks).setToUser(user); | ||
} catch (DBException e) { | ||
logger.error(e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
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
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
95 changes: 95 additions & 0 deletions
95
...in/java/com/kerrrusha/playlistassistant/dao/user_suitable_track/UserSuitableTrackDao.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,95 @@ | ||
package com.kerrrusha.playlistassistant.dao.user_suitable_track; | ||
|
||
import com.kerrrusha.playlistassistant.dao.AbstractDao; | ||
import com.kerrrusha.playlistassistant.dao.DBException; | ||
import com.kerrrusha.playlistassistant.dao.user_suitable_track.constant.*; | ||
import com.kerrrusha.playlistassistant.model.UserSuitableTrack; | ||
|
||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
public class UserSuitableTrackDao extends AbstractDao { | ||
|
||
public UserSuitableTrackDao() throws DBException {} | ||
|
||
public Collection<UserSuitableTrack> findAll() throws DBException { | ||
Collection<UserSuitableTrack> entities = new ArrayList<>(); | ||
try (Connection con = DriverManager.getConnection(FULL_URL); | ||
Statement stmt = con.createStatement(); | ||
ResultSet rs = stmt.executeQuery(Queries.SELECT_ALL_USER_SUITABLE_TRACKS)) { | ||
while(rs.next()) { | ||
entities.add(mapToUserSuitableTrack(rs)); | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new DBException(e.getMessage()); | ||
} | ||
return entities; | ||
} | ||
|
||
public Collection<UserSuitableTrack> findAllByUserId(int userId) throws DBException { | ||
Collection<UserSuitableTrack> entities = new ArrayList<>(); | ||
try (Connection con = DriverManager.getConnection(FULL_URL); | ||
PreparedStatement stmt = con.prepareStatement(Queries.FIND_USER_SUITABLE_TRACK_BY_USER_ID)) { | ||
stmt.setInt(1, userId); | ||
try (ResultSet rs = stmt.executeQuery()) { | ||
while (rs.next()) { | ||
entities.add(mapToUserSuitableTrack(rs)); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new DBException(e.getMessage()); | ||
} | ||
return entities; | ||
} | ||
|
||
public void insert(UserSuitableTrack entity) throws DBException { | ||
try (Connection con = DriverManager.getConnection(FULL_URL); | ||
PreparedStatement stmt = con.prepareStatement(Queries.INSERT_USER_SUITABLE_TRACK)) { | ||
stmt.setInt(1, entity.getUserId()); | ||
stmt.setString(2, entity.getArtistName()); | ||
stmt.setString(3, entity.getTrackName()); | ||
stmt.setString(4, entity.getTrackPhotoUrl()); | ||
stmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new DBException(e.getMessage()); | ||
} | ||
} | ||
|
||
public void deleteById(UserSuitableTrack entity) throws DBException { | ||
try (Connection con = DriverManager.getConnection(FULL_URL); | ||
PreparedStatement stmt = con.prepareStatement(Queries.DELETE_USER_SUITABLE_TRACK_BY_ID)) { | ||
stmt.setInt(1, entity.getId()); | ||
stmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new DBException(e.getMessage()); | ||
} | ||
} | ||
|
||
public void deleteByUserId(int userId) throws DBException { | ||
try (Connection con = DriverManager.getConnection(FULL_URL); | ||
PreparedStatement stmt = con.prepareStatement(Queries.DELETE_USER_SUITABLE_TRACK_BY_USER_ID)) { | ||
stmt.setInt(1, userId); | ||
stmt.executeUpdate(); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
throw new DBException(e.getMessage()); | ||
} | ||
} | ||
|
||
private UserSuitableTrack mapToUserSuitableTrack(ResultSet rs) throws SQLException { | ||
UserSuitableTrack entity = new UserSuitableTrack(); | ||
|
||
entity.setId(rs.getInt(Fields.USER_SUITABLE_TRACK_ID)); | ||
entity.setUserId(rs.getInt(Fields.USER_SUITABLE_TRACK_USER_ID)); | ||
entity.setArtistName(rs.getString(Fields.USER_SUITABLE_TRACK_ARTIST_NAME)); | ||
entity.setTrackName(rs.getString(Fields.USER_SUITABLE_TRACK_TRACK_NAME)); | ||
entity.setTrackPhotoUrl(rs.getString(Fields.USER_SUITABLE_TRACK_TRACK_PHOTO_URL)); | ||
|
||
return entity; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/kerrrusha/playlistassistant/dao/user_suitable_track/constant/Fields.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,10 @@ | ||
package com.kerrrusha.playlistassistant.dao.user_suitable_track.constant; | ||
|
||
public class Fields { | ||
|
||
public static final String USER_SUITABLE_TRACK_ID = "id"; | ||
public static final String USER_SUITABLE_TRACK_USER_ID = "user_id"; | ||
public static final String USER_SUITABLE_TRACK_ARTIST_NAME = "artist_name"; | ||
public static final String USER_SUITABLE_TRACK_TRACK_NAME = "track_name"; | ||
public static final String USER_SUITABLE_TRACK_TRACK_PHOTO_URL = "track_photo_url"; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/kerrrusha/playlistassistant/dao/user_suitable_track/constant/Queries.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,18 @@ | ||
package com.kerrrusha.playlistassistant.dao.user_suitable_track.constant; | ||
|
||
public class Queries { | ||
|
||
public static final String SELECT_ALL_USER_SUITABLE_TRACKS = "SELECT * FROM \"user_suitable_track\""; | ||
|
||
public static final String INSERT_USER_SUITABLE_TRACK = "INSERT INTO \"user_suitable_track\"(user_id, " + | ||
"artist_name, track_name, track_photo_url) VALUES (?, ?, ?, ?)"; | ||
|
||
public static final String FIND_USER_SUITABLE_TRACK_BY_USER_ID = "SELECT * FROM \"user_suitable_track\" " + | ||
"WHERE user_id = ?"; | ||
|
||
public static final String DELETE_USER_SUITABLE_TRACK_BY_ID = "DELETE FROM \"user_suitable_track\" " + | ||
"WHERE id = ?"; | ||
|
||
public static final String DELETE_USER_SUITABLE_TRACK_BY_USER_ID = "DELETE FROM \"user_suitable_track\" " + | ||
"WHERE user_id = ?"; | ||
} |
Oops, something went wrong.