Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HT-14 implementation of Pure Rest API #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

mykolaFilimonov
Copy link
Owner

  • Uniform interface used springframework.hateoas and links added to all endpoints at least every implement self link, and usually on list of all elements of the same type;
  • Client-server, we implement only server side, we do not care about client side, so we can use browser, Postman, and any what we want;
  • Layered we have simple app without additional layers between client and server
  • Stateless all services and controllers not save any data, and can extract data only by id directly from repo
  • Cachable used ShallowEtagHeaderFilter as auto implemented Etag handler, and this bullet require more learning from my side

- Uniform interface used springframework.hateoas and links added to all endpoints at least every implement self link, and usually on list of all elements of the same type;
- Client-server, we implement only server side, we do not care about client side, so we can use browser, Postman, and any what we want;
- Layered we have simple app without additional layers between client and server
- Stateless all services and controllers not save any data, and can extract data only by id directly from repo
- Cachable used ShallowEtagHeaderFilter as auto implemented Etag handler, and this bullet require more learning from my side
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@ResponseBody

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use '@RestController' instead that is itself annotated with @controller and @responsebody.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I have just done it as a test, and one more time show @RestController it is easier and more understandable than just annotation

.map(noteAssembler::toModel)
.toList();

return CollectionModel.of(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you just move this logic to NoteAssembler class?
btw, RepresentationModelAssembler has a default implementation of toCollectionModel()

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, not checked additional methods, it very helpful

@Repository
public class UserRepository {

private final static Map<String, User> users = new HashMap<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use ConcurrentHashMap, since HashMap is not thread-safe

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

user.setNotes(new ArrayList<>());
}
note.setUserUuid(userUuid);
user.getNotes().add(note);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can encapsulate this logic in the User class via the helper method and use it here instead(e.g. user.addNote(note))

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for suggestion, valid point fixed)

Comment on lines +25 to +26
if (!users.containsKey(user.getId())) {
users.put(user.getId(), user);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use here putIfAbscent method which is atomical in ConcurrentHashMap and thus there will be no race condition in between of containsKey operation and put operation.

if (user.getId() == null) {
user.setId(UUID.randomUUID().toString());
}
if (!users.containsKey(user.getId())) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an interesting part of the code in concept "what if there is already UUID in the map?". Do you expect duplicates?
If so, then better to make it in loop until unique UUID is found.

return users.values().stream().toList();
}
public void addUser(User user) {
if (user.getId() == null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can you have here non-null user ID? It's create operation, right?

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;

@org.springframework.web.bind.annotation.ControllerAdvice

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to put here full qualified name with package?

}

public List<Note> getNotes() {
return Collections.unmodifiableList(notes);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If notes field is null there will be NullPointerException thrown.


public List<Note> getAllNotes(String userUuid) {
var user = this.getUser(userUuid);
return user.getNotes() == null ? Collections.emptyList() : user.getNotes();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to handle this case in one place - getNotes method itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants