[FEAT] think about mongo
This commit is contained in:
parent
6107b95dcd
commit
1cf08390ec
34
back/src/org/kar/karusic/MorphiaService.java
Normal file
34
back/src/org/kar/karusic/MorphiaService.java
Normal file
@ -0,0 +1,34 @@
|
||||
package org.kar.karusic;
|
||||
|
||||
import org.kar.karusic.model.Album;
|
||||
import org.kar.karusic.model.Artist;
|
||||
import org.kar.karusic.model.Gender;
|
||||
import org.kar.karusic.model.Playlist;
|
||||
import org.kar.karusic.model.Track;
|
||||
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
|
||||
import dev.morphia.Datastore;
|
||||
import dev.morphia.Morphia;
|
||||
|
||||
public class MorphiaService {
|
||||
|
||||
private final Datastore datastore;
|
||||
|
||||
public MorphiaService() {
|
||||
// Connect to MongoDB
|
||||
final MongoClient mongoClient = MongoClients.create("mongodb://root:base_db_password@localhost:27017");
|
||||
this.datastore = Morphia.createDatastore(mongoClient, "karusic");
|
||||
|
||||
// Map entities
|
||||
this.datastore.getMapper().map(Album.class, Artist.class, Gender.class, Playlist.class, Track.class);
|
||||
|
||||
// Ensure indexes
|
||||
this.datastore.ensureIndexes();
|
||||
}
|
||||
|
||||
public Datastore getDatastore() {
|
||||
return this.datastore;
|
||||
}
|
||||
}
|
@ -14,11 +14,14 @@ import org.kar.archidata.dataAccess.addOn.AddOnDataJson;
|
||||
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
|
||||
import org.kar.archidata.dataAccess.options.CheckFunction;
|
||||
import org.kar.archidata.tools.DataTools;
|
||||
import org.kar.karusic.MorphiaService;
|
||||
import org.kar.karusic.model.Album;
|
||||
import org.kar.karusic.model.Album.AlbumChecker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import dev.morphia.query.Query;
|
||||
import dev.morphia.query.filters.Filters;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
@ -36,48 +39,97 @@ import jakarta.ws.rs.core.MediaType;
|
||||
public class AlbumResource {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AlbumResource.class);
|
||||
static final AlbumChecker CHECKER = new AlbumChecker();
|
||||
|
||||
|
||||
private final MorphiaService morphiaService = new MorphiaService();
|
||||
|
||||
@GET
|
||||
@Path("{id}")
|
||||
@RolesAllowed("USER")
|
||||
@Operation(description = "Get a specific Album with his ID")
|
||||
public static Album get(@PathParam("id") final Long id) throws Exception {
|
||||
return DataAccess.get(Album.class, id);
|
||||
public Album get(@PathParam("id") final Long id) throws Exception {
|
||||
//return DataAccess.get(Album.class, id);
|
||||
return this.morphiaService.getDatastore().find(Album.class).filter(Filters.eq("id", id)).first();
|
||||
}
|
||||
|
||||
|
||||
@GET
|
||||
@RolesAllowed("USER")
|
||||
@Operation(description = "Get all the available Albums")
|
||||
public List<Album> gets() throws Exception {
|
||||
return DataAccess.gets(Album.class);
|
||||
//return DataAccess.gets(Album.class);
|
||||
final Query<Album> query = this.morphiaService.getDatastore().find(Album.class);
|
||||
return query.stream().toList();
|
||||
}
|
||||
|
||||
|
||||
@POST
|
||||
@RolesAllowed("ADMIN")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Operation(description = "Add an album (when all the data already exist)")
|
||||
public Album post(final Album data) throws Exception {
|
||||
return DataAccess.insert(data, new CheckFunction(CHECKER));
|
||||
}
|
||||
// TODO: how to manage the checker ???
|
||||
final Album ret = this.morphiaService.getDatastore().save(data);
|
||||
return ret;
|
||||
/*
|
||||
|
||||
final MongoCollection<Track> trackCollection = db.getCollection("TTRACLK", Track.class);
|
||||
|
||||
final InsertOneResult res = trackCollection.insertOne(plop);
|
||||
|
||||
LOGGER.warn("plpop {}", res);
|
||||
|
||||
final ObjectId ploppppp = res.getInsertedId().asObjectId().getValue();
|
||||
|
||||
LOGGER.warn("plpop 2522 {}", res.getInsertedId().asObjectId().getValue());
|
||||
|
||||
final Track ret = trackCollection.find(Filters.eq("_id", res.getInsertedId().asObjectId().getValue()))
|
||||
|
||||
.first();
|
||||
|
||||
System.out.println("Grade found:\t" + ret);
|
||||
|
||||
return DataAccess.insert(data, new CheckFunction(CHECKER));
|
||||
*/
|
||||
}
|
||||
|
||||
@PATCH
|
||||
@Path("{id}")
|
||||
@RolesAllowed("ADMIN")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Operation(description = "Update a specific album")
|
||||
public Album patch(@PathParam("id") final Long id, @AsyncType(Album.class) final String jsonRequest) throws Exception {
|
||||
public Album patch(@PathParam("id") final Long id, @AsyncType(Album.class) final String jsonRequest)
|
||||
throws Exception {
|
||||
// final Query<Album> query = this.morphiaService.getDatastore().find(Album.class).filter(Filters.eq("id", id));
|
||||
// final UpdateOperations<Album> ops = this.morphiaService.getDatastore().createUpdateOperations(Album.class)
|
||||
// .set("name", master.getName());
|
||||
// this.morphiaService.getDatastore().update(query, ops);
|
||||
// return Response.ok(master).build();
|
||||
|
||||
DataAccess.updateWithJson(Album.class, id, jsonRequest, new CheckFunction(CHECKER));
|
||||
return DataAccess.get(Album.class, id);
|
||||
}
|
||||
|
||||
// @PUT
|
||||
// @Path("{id}")
|
||||
// @RolesAllowed("ADMIN")
|
||||
// @Consumes(MediaType.APPLICATION_JSON)
|
||||
// @Operation(description = "Update a specific album")
|
||||
// public Album put(@PathParam("id") final Long id, final Album album)
|
||||
// throws Exception {
|
||||
// final Query<Album> query = this.morphiaService.getDatastore().find(Album.class).filter(Filters.eq("id", id));
|
||||
// final UpdateOperations<Album> ops = this.morphiaService.getDatastore().createUpdateOperations(Album.class)
|
||||
// .set("name", album.getName());
|
||||
// this.morphiaService.getDatastore().update(query, ops);
|
||||
// return Response.ok(album).build();
|
||||
// }
|
||||
|
||||
@DELETE
|
||||
@Path("{id}")
|
||||
@RolesAllowed("ADMIN")
|
||||
@Operation(description = "Remove a specific album")
|
||||
public void remove(@PathParam("id") final Long id) throws Exception {
|
||||
DataAccess.delete(Album.class, id);
|
||||
//DataAccess.delete(Album.class, id);
|
||||
this.morphiaService.getDatastore().find(Album.class).filter(Filters.eq("id", id)).delete();
|
||||
}
|
||||
|
||||
|
||||
@POST
|
||||
@Path("{id}/track/{trackId}")
|
||||
@RolesAllowed("ADMIN")
|
||||
@ -87,23 +139,17 @@ public class AlbumResource {
|
||||
AddOnManyToMany.removeLink(Album.class, id, "track", trackId);
|
||||
return DataAccess.get(Album.class, id);
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{id}/track/{trackId}")
|
||||
@RolesAllowed("ADMIN")
|
||||
@Operation(description = "Remove a Track on a specific album")
|
||||
public Album removeTrack(@PathParam("id") final Long id, @PathParam("trackId") final Long trackId) throws Exception {
|
||||
AddOnManyToMany.removeLink(Album.class, id, "track", trackId);
|
||||
return DataAccess.get(Album.class, id);
|
||||
}
|
||||
|
||||
|
||||
@POST
|
||||
@Path("{id}/cover")
|
||||
@RolesAllowed("ADMIN")
|
||||
@Consumes({ MediaType.MULTIPART_FORM_DATA })
|
||||
@Operation(description = "Add a cover on a specific album")
|
||||
@TypeScriptProgress
|
||||
public Album uploadCover(@PathParam("id") final Long id, @FormDataOptional @FormDataParam("uri") final String uri, @FormDataOptional @FormDataParam("file") final InputStream fileInputStream,
|
||||
public Album uploadCover(
|
||||
@PathParam("id") final Long id,
|
||||
@FormDataOptional @FormDataParam("uri") final String uri,
|
||||
@FormDataOptional @FormDataParam("file") final InputStream fileInputStream,
|
||||
@FormDataOptional @FormDataParam("file") final FormDataContentDisposition fileMetaData) throws Exception {
|
||||
if (uri != null) {
|
||||
DataTools.uploadCoverFromUri(Album.class, id, uri);
|
||||
@ -112,12 +158,13 @@ public class AlbumResource {
|
||||
}
|
||||
return DataAccess.get(Album.class, id);
|
||||
}
|
||||
|
||||
|
||||
@DELETE
|
||||
@Path("{id}/cover/{coverId}")
|
||||
@RolesAllowed("ADMIN")
|
||||
@Operation(description = "Remove a cover on a specific album")
|
||||
public Album removeCover(@PathParam("id") final Long id, @PathParam("coverId") final UUID coverId) throws Exception {
|
||||
public Album removeCover(@PathParam("id") final Long id, @PathParam("coverId") final UUID coverId)
|
||||
throws Exception {
|
||||
AddOnDataJson.removeLink(Album.class, id, "covers", coverId);
|
||||
return DataAccess.get(Album.class, id);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user