Andrzej GoĊ‚awski

Warszawa 12-07-2022 @ <WJUG>

class User {

    private UUID id;
    private String name;
    private String lastName;
    private Address address;

}
class Address {

    private String street;
    private String city;
    private String country;

}
public class UserController {

    @PostMapping
    public UserDTO createUser(UserDTO user) {
        //create user
    }

    @PutMapping("{id}")
    public UserDTO updateUser(UUID id, UserDTO user) {
        //update user
    }

    @GetMapping("{id}")
    public UserDTO findUser(UUID id) {
        //find user
    }

}
public class UserDTO {

    private UUID id;
    private String name;
    private String lastName;
    private AddressDTO address;

}
public class AddressDTO {

    private String street;
    private String city;
    private String country;

}
public class UserController {

    @PutMapping("{id}")
    public ResponseEntity<UserDTO> createUser(UUID id, UserDTO user) {
        if(isNull(user.getId())) {
            //update user
        } else {
            // return 400
        }
    }

}

https://blog.scottlogic.com/2020/01/03/rethinking-the-java-dto.html

public class UserApi {

    public static class Create {

        private String name;
        private String lastName;
        private Address address;

    }

    public static class User {

        private UUID id;
        private String name;
        private String lastName;
        private Address address;

    }

    public static class Address {

        private String street;
        private String city;
        private String country;

    }

}
public class UserController {

    @PostMapping
    public UserApi.User createUser(UserApi.Create createUser) {
        //create user
    }
    
    @PutMapping
    public UserApi.User updateUser(UserApi.Update updateUser) {
        //update user
    }

    @GetMapping("{id}")
    public UserApi.User findUser(UUID id) {
        //find user
    }

}
public class UserApi {

    private interface Name { @NotBlank String getName(); }
    private interface LastName { @NotBlank String getLastName(); }
   

    public static class Create implements Name, LastName {

        private String name;
        private String lastName;
        private Address address;

    }

    public static class Update implements Name, LastName {

        private String name;
        private String lastName;
        private Address address;

    }

    ...

}
public class UserApi {

    private interface Name { @NotBlank String getName(); }
    private interface LastName { @NotBlank String getLastName(); }

    public enum Request{;

        public static class Create implements Name, LastName {

            private String name;
            private String lastName;
            private Address address;

        }

        public static class Update implements Name, LastName  {

            private String name;
            private String lastName;
            private Address address;

        }

    }

    public enum Response {;

        public static class User {

            private UUID id;
            private String name;
            private String lastName;
            private Address address;

        }

    }

}
public class UserController {

    @PostMapping
    public UserApi.Response.User createUser(UserApi.Request.Create createUser) {
        //create user
    }

    @PutMapping("{id}")
    public UserApi.Response.User updateUser(UUID id, UserApi.Request.Update user) {
        //update user
    }

    @GetMapping("{id}")
    public UserApi.Response.User findUser(UUID id) {
        //find user
    }

}

Thank you

Made with Slides.com