Skip to content

Commit

Permalink
Admin UI: Add Banner system
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyKarnbach authored and andresTabiTuwien committed Jan 21, 2025
1 parent 99c76fa commit dfde7b2
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/main/java/org/damap/base/domain/Banner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.damap.base.domain;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/** Banner class. */
@Data
@EqualsAndHashCode(callSuper = true)
@ToString
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Banner extends PanacheEntity {

@Column(name = "title")
private String title;

@Column(name = "description")
private String description;

@Column(name = "dismissible")
private Boolean dismissible;

@Column(name = "color")
private String color;
}
54 changes: 54 additions & 0 deletions src/main/java/org/damap/base/rest/AdminResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.damap.base.rest;

import io.quarkus.security.Authenticated;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import lombok.extern.jbosslog.JBossLog;
import org.damap.base.rest.admin.domain.BannerDO;
import org.damap.base.rest.admin.service.AdminService;

/** AdminResource class. */
@Path("/api/admin")
@Produces(MediaType.APPLICATION_JSON)
@Authenticated
@JBossLog
public class AdminResource {

@Inject AdminService adminService;

@GET
@Path("/banner")
public BannerDO getAppBanner() {
log.info("GET /admin/banner");
return this.adminService.getAppBanner();
}

@POST
@Path("/banner")
@RolesAllowed("Damap Admin")
public BannerDO createAppBanner(@Valid BannerDO bannerDO) {
log.info("POST /admin/banner");
log.info(bannerDO);
return this.adminService.createAppBanner(bannerDO);
}

@PUT
@Path("/banner")
@RolesAllowed("Damap Admin")
public BannerDO updateAppBanner(@Valid BannerDO bannerDO) {
log.info("PUT /admin/banner");
log.info(bannerDO);
return this.adminService.updateAppBanner(bannerDO);
}

@DELETE
@Path("/banner")
@RolesAllowed("Damap Admin")
public void deleteAppBanner() {
log.info("DELETE /admin/banner");
this.adminService.deleteAppBanner();
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/damap/base/rest/admin/domain/BannerDO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.damap.base.rest.admin.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.validation.constraints.Size;
import lombok.Data;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class BannerDO {

@Size(max = 255)
private String title;

@Size(max = 255)
private String description;

private Boolean dismissible;

@Size(max = 255)
private String color;
}
42 changes: 42 additions & 0 deletions src/main/java/org/damap/base/rest/admin/mapper/BannerDOMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.damap.base.rest.admin.mapper;

import lombok.experimental.UtilityClass;
import org.damap.base.domain.Banner;
import org.damap.base.rest.admin.domain.BannerDO;

/** BannerDOMapper class. */
@UtilityClass
public class BannerDOMapper {

/**
* mapEntityToDO.
*
* @param banner a {@link Banner} object
* @param bannerDO a {@link BannerDO} object
* @return a {@link BannerDO} object
*/
public BannerDO mapEntityToDO(Banner banner, BannerDO bannerDO) {
bannerDO.setTitle(banner.getTitle());
bannerDO.setDescription(banner.getDescription());
bannerDO.setDismissible(banner.getDismissible());
bannerDO.setColor(banner.getColor());

return bannerDO;
}

/**
* mapDOtoEntity.
*
* @param bannerDO a {@link BannerDO} object
* @param banner a {@link Banner} object
* @return a {@link BannerDO} object
*/
public Banner mapDOtoEntity(BannerDO bannerDO, Banner banner) {
banner.setTitle(bannerDO.getTitle());
banner.setDescription(bannerDO.getDescription());
banner.setDismissible(bannerDO.getDismissible());
banner.setColor(bannerDO.getColor());

return banner;
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/damap/base/rest/admin/service/AdminService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.damap.base.rest.admin.service;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import java.util.*;
import lombok.extern.jbosslog.JBossLog;
import org.damap.base.domain.Banner;
import org.damap.base.rest.admin.domain.BannerDO;
import org.damap.base.rest.admin.mapper.BannerDOMapper;
import org.damap.base.rest.dmp.mapper.*;

@ApplicationScoped
@JBossLog
public class AdminService {

public BannerDO getAppBanner() {
Banner banner = Banner.findAll().firstResult();
if (banner == null) {
return null;
}
return BannerDOMapper.mapEntityToDO(banner, new BannerDO());
}

@Transactional
public BannerDO createAppBanner(@Valid BannerDO bannerDO) {
long existingBannerCount = Banner.count();
if (existingBannerCount > 0) {
throw new IllegalStateException("There is already a banner in the database");
}

Banner banner = BannerDOMapper.mapDOtoEntity(bannerDO, new Banner());
banner.persist();

return bannerDO;
}

@Transactional
public BannerDO updateAppBanner(@Valid BannerDO bannerDO) {
Banner banner = Banner.findAll().firstResult();
if (banner == null) {
throw new IllegalStateException("There is no banner in the database");
}

banner = BannerDOMapper.mapDOtoEntity(bannerDO, banner);
banner.persist();

return bannerDO;
}

@Transactional
public void deleteAppBanner() {
Banner banner = Banner.findAll().firstResult();
if (banner == null) {
throw new IllegalStateException("There is no banner in the database");
}

banner.delete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
databaseChangeLog:
- changeSet:
id: 17
author: Geoffrey Karnbach
changes:
- createSequence:
sequenceName: banner_seq
startValue: 1
incrementBy: 1

- createTable:
tableName: banner
columns:
- column:
name: id
type: BIGINT
defaultValueComputed: "nextval('banner_seq')"
constraints:
primaryKey: true
nullable: false
unique: true
- column:
name: title
type: VARCHAR(255)
constraints:
nullable: true
- column:
name: description
type: VARCHAR(255)
constraints:
nullable: true
- column:
name: dismissible
type: BOOLEAN
constraints:
nullable: true
- column:
name: color
type: VARCHAR(255)
constraints:
nullable: true

rollback:
- dropTable:
tableName: banner
- dropSequence:
sequenceName: banner_seq
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ databaseChangeLog:
- include:
file: org/damap/base/db/changeLog-4.x/changeLog-4.3.0_1.yaml
- include:
file: org/damap/base/db/changeLog-4.x/changeLog-4.3.0_2.yaml
file: org/damap/base/db/changeLog-4.x/changeLog-4.3.0_2.yaml
- include:
file: org/damap/base/db/changeLog-4.x/changeLog-4.3.0_3.yaml

0 comments on commit dfde7b2

Please sign in to comment.