-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
624 additions
and
444 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/it/eng/idsa/businesslogic/entity/AuditLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package it.eng.idsa.businesslogic.entity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
@Entity | ||
@Table(name = "AuditLogs") | ||
public class AuditLog { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
@JsonProperty("id") | ||
private Long id; | ||
@JsonProperty("timestamp") | ||
private LocalDateTime timestamp; | ||
@JsonProperty("event") | ||
@Column(columnDefinition = "TEXT") | ||
private String event; | ||
|
||
public AuditLog() { | ||
} | ||
|
||
public AuditLog(String event) { | ||
this.event = event; | ||
this.timestamp = LocalDateTime.now(); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public LocalDateTime getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(LocalDateTime timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public String getEvent() { | ||
return event; | ||
} | ||
|
||
public void setEvent(String event) { | ||
this.event = event; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/it/eng/idsa/businesslogic/repository/AuditEventRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package it.eng.idsa.businesslogic.repository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import it.eng.idsa.businesslogic.entity.AuditLog; | ||
|
||
public interface AuditEventRepository extends JpaRepository<AuditLog, Long> { | ||
List<AuditLog> findByTimestampBetween(LocalDateTime startOfDay, LocalDateTime endOfDay); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/it/eng/idsa/businesslogic/service/AuditEventService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package it.eng.idsa.businesslogic.service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import it.eng.idsa.businesslogic.entity.AuditLog; | ||
import it.eng.idsa.businesslogic.repository.AuditEventRepository; | ||
|
||
@Service | ||
public class AuditEventService { | ||
@Autowired | ||
private AuditEventRepository auditRepository; | ||
|
||
public AuditLog saveAuditEvent(AuditLog auditEvent) { | ||
return auditRepository.save(auditEvent); | ||
} | ||
|
||
public List<AuditLog> getAllAuditEvents() { | ||
return auditRepository.findAll(); | ||
} | ||
|
||
public List<AuditLog> getAuditEventsForDate(LocalDate date) { | ||
LocalDateTime startOfDay = date.atStartOfDay(); // Start of the day | ||
LocalDateTime endOfDay = date.atTime(LocalTime.MAX); // End of the day | ||
|
||
return auditRepository.findByTimestampBetween(startOfDay, endOfDay); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/it/eng/idsa/businesslogic/web/rest/resources/AuditController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package it.eng.idsa.businesslogic.web.rest.resources; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import it.eng.idsa.businesslogic.entity.AuditLog; | ||
import it.eng.idsa.businesslogic.service.AuditEventService; | ||
|
||
@Tag(name = "Audit controller", description = "Returns audit logs.") | ||
@RestController | ||
@RequestMapping("/api/audit/") | ||
public class AuditController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AuditController.class); | ||
|
||
private AuditEventService auditService; | ||
|
||
public AuditController(AuditEventService auditService) { | ||
this.auditService = auditService; | ||
} | ||
|
||
@Operation(summary = "All audit logs", tags = "Audit controller - all audit logs") | ||
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "All audit logs", content = { | ||
@Content(mediaType = "application/json", schema = @Schema(implementation = AuditLog.class)) }) }) | ||
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseBody | ||
public ResponseEntity<List<AuditLog>> getAuditLogs( | ||
@RequestParam(value = "date", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) { | ||
if (date != null) { | ||
logger.info("Fetching audit logs for date: {}", date); | ||
return ResponseEntity.ok(auditService.getAuditEventsForDate(date)); | ||
} else { | ||
logger.info("Fetching all audit logs"); | ||
return ResponseEntity.ok(auditService.getAllAuditEvents()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.