Ejemplo n.º 1
0
  @POST
  @UnitOfWork
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Path("/{name}/add")
  public Response addMusic(
      @PathParam("name") String name,
      @FormParam("music_id") IntParam musicId,
      @FormParam("number") IntParam number)
      throws URISyntaxException {

    if (musicId == null || number == null || number.get() < 1) {
      Response.status(Response.Status.BAD_REQUEST).build();
    }
    final List<PlayList> playLists = playListDAO.findByName(name);
    if (!playLists.isEmpty()) {
      Response.status(Response.Status.NOT_FOUND).build();
    }
    Optional<Music> m = musicDAO.findById(musicId.get());
    if (m.get() == null) {
      Response.status(Response.Status.BAD_REQUEST).build();
    }
    List<PlayListDetail> list = playListDetailDAO.findByName(name);
    int num = 1;
    for (PlayListDetail d : list) {
      if (number.get().equals(num)) {
        PlayListDetail nd = new PlayListDetail();
        nd.setMusic_id(musicId.get());
        nd.setPlaylist_name(name);
        nd.setNumber(num);
        playListDetailDAO.create(nd);
        num++;
      }
      d.setNumber(num);
      playListDetailDAO.create(d);
      num++;
    }

    return Response.status(Response.Status.NO_CONTENT).build();
  }
Ejemplo n.º 2
0
  @GET
  @UnitOfWork
  @Path("/{name}")
  public Response getMusic(@PathParam("name") String name) {
    final List<PlayList> playLists = playListDAO.findByName(name);
    if (playLists.isEmpty()) {
      return Response.status(Response.Status.NOT_FOUND).build();
    }

    //		for (PlayList playList : playLists) {
    //			List<PlayListDetail> list = playListDetailDAO.findByName(name);
    //			playList.setPlayListDetails(list);
    //		}
    List<PlayListDetail> list = playListDetailDAO.findByName(name);
    List<Music> musics = new ArrayList<>();
    for (PlayListDetail d : list) {
      if (d.getMusic_id() != null) {
        Optional<Music> m = musicDAO.findById(d.getMusic_id());
        musics.add(m.get());
      }
    }
    playLists.get(0).setMusics(musics);
    return Response.status(Response.Status.OK).entity(playLists).build();
  }