Added date constrain

This commit is contained in:
Marco Cetica 2024-01-23 09:17:24 +01:00
parent 7c603e8ea2
commit 843da9d81c
Signed by: marco
GPG Key ID: 45060A949E90D0FD
3 changed files with 21 additions and 8 deletions

View File

@ -2,17 +2,17 @@
Bit is a simple, web-based, self-hostable text sharing platform written in Java and Spring.
You can access it [from here](https://bit.marcocetica.com).
The frontend is also open-source and it's available [on this page](https://github.com/ceticamarco/bit_frontend).
The frontend is also open-source, and it's available [on this page](https://github.com/ceticamarco/bit_frontend).
## General
**Bit** can be used both from the [frontend](https://bit.marcocetica.com) and through the REST API.
Before using it, read the following technical notices:
1. By default, each new _"text"_(from now on: **post**) added to the platform is anonymous and does not expire.
In order to associate a new post to an identity, you first need to register a new user account.
The registration process requires a unique **username**, a unique **email address** and a **password**.
User accounts can **NOT** be modified or recovered; therefore if you lose your password, you will need
to create a new account using a new email address.
1. By default, each new _"text"_(from now on: **post**) added to the platform
is anonymous and expires after one week. In order to associate a new post to an identity,
you first need to register a new user account. The registration process requires a unique **username**,
a unique **email address** and a **password**. User accounts can **NOT** be modified or recovered;
therefore if you lose your password, you will need to create a new account using a new email address.
2. Each user account is associated with a _user role_ that determines the granular access
to the _special_ endpoints. The _user role_ is set by default to **unprivileged** at registration
@ -188,7 +188,7 @@ Below there are some practical examples on how to use the REST API:
}
```
In this case user `john@example.com` is an user of the class `PRIVILEGED`.
In this case user `john@example.com` is a user of the class `PRIVILEGED`.
## Unit tests
The **bit** platform provides some unit tests for the _post_ and the _user_ controllers. You can

View File

@ -33,7 +33,7 @@ public class Post {
@Column(name = "created_at", nullable = false, updatable = false)
private LocalDate createdAt;
@Column(name = "expiration_date")
@Column(name = "expiration_date", nullable = false)
@Future(message = "expiration date must be in the future")
private LocalDate expirationDate;
@ -60,4 +60,6 @@ public class Post {
public void setTitle(String title) { this.title = title; }
public void setContent(String content) { this.content = content; }
public void setExpirationDate(LocalDate expirationDate) { this.expirationDate = expirationDate; }
}

View File

@ -117,6 +117,17 @@ public class PostService {
post.setUser(null);
}
// If expiration date is not specified, set it to one week from now
if(post.getExpirationDate() == null) {
var currentDate = LocalDate.now();
post.setExpirationDate(currentDate.plusWeeks(1));
}
// Discard posts with expiration date greater than the current year
if(post.getExpirationDate().getYear() > LocalDate.now().getYear()) {
return Either.left(new Error("expiration date must be within this year"));
}
// Save the post into the database and return its ID
var postId = postRepository.save(post).getId();