Skip to content

Commit

Permalink
v1.0 RELEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Koval committed Nov 22, 2022
1 parent b0511f3 commit 8f3ed5a
Show file tree
Hide file tree
Showing 37 changed files with 837 additions and 147 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
viewModel.setArtists(SoundDataProvider.getInstance().getPresentableTopGenreArtists().shuffle());

request.setAttribute("model", viewModel);
request.getRequestDispatcher("set-favourite-artists.jsp").forward(request, response);
request.getRequestDispatcher("select-favourite-artists.jsp").forward(request, response);
}
}
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);
}
}
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);
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import java.util.ArrayList;
import java.util.Collection;

public class UserSelectedArtistDao extends AbstractDao {
public class UserSelectedArtistsDao extends AbstractDao {

public UserSelectedArtistDao() throws DBException {}
public UserSelectedArtistsDao() throws DBException {}

public Collection<UserSelectedArtist> findAll() throws DBException {
Collection<UserSelectedArtist> entities = new ArrayList<>();
try (Connection con = DriverManager.getConnection(FULL_URL);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(Queries.SELECT_ALL_USERS_SELECTED_ARTISTS);) {
ResultSet rs = stmt.executeQuery(Queries.SELECT_ALL_USER_SELECTED_ARTISTS)) {
while(rs.next()) {
entities.add(mapToUserSelectedArtist(rs));
}
Expand All @@ -32,10 +32,10 @@ public Collection<UserSelectedArtist> findAll() throws DBException {
public Collection<UserSelectedArtist> findAllByUserId(int userId) throws DBException {
Collection<UserSelectedArtist> entities = new ArrayList<>();
try (Connection con = DriverManager.getConnection(FULL_URL);
PreparedStatement stmt = con.prepareStatement(Queries.FIND_USER_SELECTED_ARTISTS_BY_USER_ID)) {
PreparedStatement stmt = con.prepareStatement(Queries.FIND_USER_SELECTED_ARTIST_BY_USER_ID)) {
stmt.setInt(1, userId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
while (rs.next()) {
entities.add(mapToUserSelectedArtist(rs));
}
}
Expand All @@ -51,6 +51,7 @@ public void insert(UserSelectedArtist entity) throws DBException {
PreparedStatement stmt = con.prepareStatement(Queries.INSERT_USER_SELECTED_ARTIST)) {
stmt.setInt(1, entity.getUserId());
stmt.setString(2, entity.getArtistName());
stmt.setString(3, entity.getArtistPhotoUrl());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
Expand All @@ -69,12 +70,24 @@ public void deleteById(UserSelectedArtist entity) throws DBException {
}
}

public void deleteByUserId(int userId) throws DBException {
try (Connection con = DriverManager.getConnection(FULL_URL);
PreparedStatement stmt = con.prepareStatement(Queries.DELETE_USER_SELECTED_ARTIST_BY_USER_ID)) {
stmt.setInt(1, userId);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new DBException(e.getMessage());
}
}

private UserSelectedArtist mapToUserSelectedArtist(ResultSet rs) throws SQLException {
UserSelectedArtist userSelectedArtist = new UserSelectedArtist();

userSelectedArtist.setId(rs.getInt(Fields.USER_SELECTED_ARTIST_ID));
userSelectedArtist.setUserId(rs.getInt(Fields.USER_SELECTED_ARTIST_USER_ID));
userSelectedArtist.setArtistName(rs.getString(Fields.USER_SELECTED_ARTIST_ARTIST_NAME));
userSelectedArtist.setArtistPhotoUrl(rs.getString(Fields.USER_SELECTED_ARTIST_ARTIST_PHOTO_URL));

return userSelectedArtist;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public class Fields {
public static final String USER_SELECTED_ARTIST_ID = "id";
public static final String USER_SELECTED_ARTIST_USER_ID = "user_id";
public static final String USER_SELECTED_ARTIST_ARTIST_NAME = "artist_name";

public static final String USER_SELECTED_ARTIST_ARTIST_PHOTO_URL = "artist_photo_url";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

public class Queries {

public static final String SELECT_ALL_USERS_SELECTED_ARTISTS = "SELECT * FROM \"user_selected_artist\"";
public static final String SELECT_ALL_USER_SELECTED_ARTISTS = "SELECT * FROM \"user_selected_artist\"";

public static final String INSERT_USER_SELECTED_ARTIST = "INSERT INTO \"user_selected_artist\"(user_id, artist_name) VALUES (?, ?)";
public static final String INSERT_USER_SELECTED_ARTIST = "INSERT INTO \"user_selected_artist\"(user_id, artist_name, artist_photo_url)" +
" VALUES (?, ?, ?)";

public static final String FIND_USER_SELECTED_ARTISTS_BY_USER_ID = "SELECT * FROM \"user_selected_artist\" WHERE user_id = ?";
public static final String FIND_USER_SELECTED_ARTIST_BY_USER_ID = "SELECT * FROM \"user_selected_artist\" WHERE user_id = ?";

public static final String DELETE_USER_SELECTED_ARTIST_BY_ID = "DELETE FROM \"user_selected_artist\" WHERE id = ?";

public static final String DELETE_USER_SELECTED_ARTIST_BY_USER_ID = "DELETE FROM \"user_selected_artist\" WHERE user_id = ?";
}
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;
}
}
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";
}
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 = ?";
}
Loading

0 comments on commit 8f3ed5a

Please sign in to comment.