CrudUser3Resource.java
/*
* Copyright © 2024-2025 The CTAN Team and individual authors
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.resources.admin;
import java.time.LocalDateTime;
import java.util.Map;
import org.ctan.site.domain.account.User;
import org.ctan.site.stores.UserStore;
import org.ctan.site.stores.base.GeneralPage;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.dropwizard.hibernate.UnitOfWork;
import jakarta.annotation.security.RolesAllowed;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.DefaultValue;
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.QueryParam;
import jakarta.ws.rs.core.MediaType;
import lombok.NonNull;
/**
* The class <code>CrudUser3Resource</code> contains the CRUD controller for the
* user resource.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
@Path("/3.0/admin")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed("ADMIN")
public class CrudUser3Resource {
/**
* The field <code>store</code> contains the underlying store.
*/
private UserStore store;
/**
* This is the constructor for the class <code>CrudUser3Resource</code>.
*
* @param store the underlying store
*/
@SuppressFBWarnings(value = {"CT_CONSTRUCTOR_THROW", "EI_EXPOSE_REP2"})
public CrudUser3Resource(@NonNull UserStore store) {
this.store = store;
}
/**
* The method <code>create</code> provides an end-point to create a user.
*
* @param user the user object to store
* @return the updated entity
*/
@POST
@Path("/user")
@UnitOfWork(value = "siteDb")
public User create(@NonNull User user) {
user.setDateCreated(LocalDateTime.now());
return store.save(user);
}
/**
* The method <code>get</code> provides means to retrieve a user.
*
* @param id the id
* @return {@code true} iff the user exists
*/
@GET
@Path("/user/id/{id}")
@UnitOfWork(value = "siteDb")
public User get(@NonNull @PathParam("id") Long id) {
return store.getById(id);
}
/**
* The method <code>get</code> provides means to retrieve a user.
*
* @param account the account
* @return {@code true} iff the user exists
*/
@GET
@Path("/user/{account}")
@UnitOfWork(value = "siteDb")
public User get(@NonNull @PathParam("account") String account) {
return store.getByAccount(account);
}
/**
* The method <code>list</code> provides means to retrieve a page of users.
*
* @param q the name pattern
* @param page the page
* @param size the page size
* @param orderBy the order
* @param asc the indicator for ascending
* @return the page for the user or {@code null}
*/
@GET
@Path("/users")
@UnitOfWork(value = "siteDb")
public GeneralPage list(@QueryParam("q") String q,
@QueryParam("page") @DefaultValue("1") int page,
@QueryParam("size") @DefaultValue("256") int size,
@QueryParam("order") String orderBy,
@QueryParam("asc") boolean asc) {
return store.list(q, page - 1, size, orderBy, asc);
}
/**
* The method <code>remove</code> provides an end-point to remove a user.
*
* @param id the id
* @return {@code true} iff the user has existed
*/
@DELETE
@Path("/user/id/{id}")
@UnitOfWork(value = "siteDb")
public boolean remove(@NonNull @PathParam("id") Long id) {
var user = get(id);
if (user == null) {
return false;
}
store.remove(user);
return true;
}
/**
* The method <code>remove</code> provides an end-point to remove a user.
*
* @param account the account
* @return {@code true} iff the user has existed
*/
@DELETE
@Path("/user/{account}")
@UnitOfWork(value = "siteDb")
public boolean remove(@NonNull @PathParam("account") String account) {
return store.removeByAccount(account);
}
/**
* The method <code>set</code> provides means to set a single attribute of a
* user.
*
* @param account the account
* @param map the mapping containing key and value
* @return {@code true} iff the setting has succeeded
*/
@PUT
@Path("/user/{account}")
@UnitOfWork(value = "siteDb")
public boolean set(@NonNull @PathParam("account") String account,
@NonNull Map<String, String> map) {
String key = map.get("key");
String value = map.get("value");
if (key == null || value == null) {
return false;
}
return store.set(account, key, value);
}
}