100 lines
3.2 KiB
Java
100 lines
3.2 KiB
Java
package org.atriasoft.karusic.api;
|
|
|
|
import java.io.InputStream;
|
|
import java.util.List;
|
|
|
|
import org.atriasoft.archidata.annotation.apiGenerator.ApiInputOptional;
|
|
import org.atriasoft.archidata.annotation.apiGenerator.ApiTypeScriptProgress;
|
|
import org.atriasoft.archidata.dataAccess.DBAccess;
|
|
import org.atriasoft.archidata.dataAccess.DataAccess;
|
|
import org.atriasoft.archidata.dataAccess.addOnSQL.AddOnDataJson;
|
|
import org.atriasoft.archidata.tools.DataTools;
|
|
import org.atriasoft.karusic.model.Gender;
|
|
import org.bson.types.ObjectId;
|
|
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
|
|
import org.glassfish.jersey.media.multipart.FormDataParam;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import jakarta.annotation.security.RolesAllowed;
|
|
import jakarta.validation.Valid;
|
|
import jakarta.ws.rs.Consumes;
|
|
import jakarta.ws.rs.DELETE;
|
|
import jakarta.ws.rs.GET;
|
|
import jakarta.ws.rs.POST;
|
|
import jakarta.ws.rs.PUT;
|
|
import jakarta.ws.rs.Path;
|
|
import jakarta.ws.rs.PathParam;
|
|
import jakarta.ws.rs.Produces;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
|
|
@Path("/gender")
|
|
@Produces({ MediaType.APPLICATION_JSON })
|
|
public class GenderResource {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(GenderResource.class);
|
|
|
|
@GET
|
|
@Path("{oid}")
|
|
@RolesAllowed("USER")
|
|
public Gender get(@PathParam("oid") final ObjectId oid) throws Exception {
|
|
return DataAccess.get(Gender.class, oid);
|
|
}
|
|
|
|
@GET
|
|
@RolesAllowed("USER")
|
|
public List<Gender> gets() throws Exception {
|
|
return DataAccess.gets(Gender.class);
|
|
}
|
|
|
|
@POST
|
|
@RolesAllowed("ADMIN")
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
public Gender post(@Valid final Gender data) throws Exception {
|
|
return DataAccess.insert(data);
|
|
}
|
|
|
|
@PUT
|
|
@Path("{oid}")
|
|
@RolesAllowed("ADMIN")
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
public Gender patch(@PathParam("oid") final ObjectId oid, @Valid final Gender gender) throws Exception {
|
|
gender.oid = oid;
|
|
DataAccess.update(gender, oid);
|
|
return DataAccess.get(Gender.class, oid);
|
|
}
|
|
|
|
@DELETE
|
|
@Path("{oid}")
|
|
@RolesAllowed("ADMIN")
|
|
public void remove(@PathParam("oid") final ObjectId oid) throws Exception {
|
|
DataAccess.delete(Gender.class, oid);
|
|
}
|
|
|
|
@POST
|
|
@Path("{oid}/cover")
|
|
@RolesAllowed("ADMIN")
|
|
@Consumes({ MediaType.MULTIPART_FORM_DATA })
|
|
@ApiTypeScriptProgress
|
|
public Gender uploadCover(@PathParam("oid") final ObjectId oid, @ApiInputOptional @FormDataParam("uri") final String uri,
|
|
@ApiInputOptional @FormDataParam("file") final InputStream fileInputStream, @ApiInputOptional @FormDataParam("file") final FormDataContentDisposition fileMetaData) throws Exception {
|
|
try (DBAccess db = DBAccess.createInterface()) {
|
|
if (uri != null) {
|
|
DataTools.uploadCoverFromUri(db, Gender.class, oid, uri);
|
|
} else {
|
|
DataTools.uploadCover(db, Gender.class, oid, fileInputStream, fileMetaData);
|
|
}
|
|
return db.get(Gender.class, oid);
|
|
}
|
|
}
|
|
|
|
@DELETE
|
|
@Path("{oid}/cover/{coverId}")
|
|
@RolesAllowed("ADMIN")
|
|
public Gender removeCover(@PathParam("oid") final ObjectId oid, @PathParam("coverId") final ObjectId coverId) throws Exception {
|
|
try (DBAccess db = DBAccess.createInterface()) {
|
|
AddOnDataJson.removeLink(db, Gender.class, "_id", oid, "covers", coverId);
|
|
return db.get(Gender.class, oid);
|
|
}
|
|
}
|
|
}
|