My account.java is this
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@Document(collection="Account")
public class Account {
@Id
private String id;
private String username;
private String password;
private String role;
}
my repo for it is this
import org.springframework.data.mongodb.repository.MongoRepository;
public interface AccountRepository extends MongoRepository<Account, Integer> {
}
Finally. For my controller (not done yet)
@PutMapping("/createAccount")
public void createAccount(@RequestBody Account account) {
}
I want the following,
If I were to send a json in the request body such as
{
"username": "Tom",
"password": "123456",
"role": "Employee"
}
Then it would make an object in the Account collection with that property which can easily be done through repository.insert(account). However I need to check certain filters
- All 3 objects needs to be set
- role must be either "Employee", "Admin", or "Customer"
- Lastly user isn't already in the database
Otherwise sends Response 400
How do I achieve this with springboot?