0

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

  1. All 3 objects needs to be set
  2. role must be either "Employee", "Admin", or "Customer"
  3. Lastly user isn't already in the database

Otherwise sends Response 400

How do I achieve this with springboot?

bob
  • 75
  • 4
  • 1
    for 1 and 2 check this https://stackoverflow.com/questions/6294587/java-string-validation-using-enum-values-and-annotation for 3 you have to do it manuallym by reading into database – pvpkiran Oct 10 '19 at 08:04
  • > for 3 you have to do it manuallym by reading into database - how? – bob Oct 10 '19 at 21:11
  • 1
    Add a repository method 'findByUserName' and pass the user name . Check if the result is empty only the. Proceed with save – pvpkiran Oct 10 '19 at 21:14
  • okay that works but is there a list of these commands? Additionally, if you know, how would I accomplish custom queries? For example, db.getCollection("Account").find({"username":"bob123"}) (I know this can easily be done with these custom methods just using it as an example) – bob Oct 10 '19 at 21:40
  • 1
    @bob for custom queries https://www.baeldung.com/spring-data-jpa-query There are also default queries provided by the @ Repository annotation. Example of queries provided by spring boot https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference – fctmolina Oct 11 '19 at 01:46

1 Answers1

0
  1. You can use @Valid Annotation in the Controller then you can put either @NotNull or @NotEmpty or @NotBlank in your Model class. Reference: https://www.baeldung.com/spring-boot-bean-validation

public void createAccount(@Valid @RequestBody Account account) {

}

public class Account {

    @Id
    @NotNull
    private String id;
    @NotNull
    private String username;
    @NotNull
    private String password;
    @NotNull
    private String role;
}
  1. You can use Spring Security Expressions. Reference: https://www.baeldung.com/spring-security-expressions-basic

    @PreAuthorize("hasRole('ROLE_ADMIN')")

    @PutMapping("/createAccount")

    public void createAccount(@RequestBody Account account) {

    }

fctmolina
  • 196
  • 2
  • 10