Skip to content

Commit

Permalink
Change Card relationship to OneToOne with User
Browse files Browse the repository at this point in the history
  • Loading branch information
falvojr committed Jul 15, 2023
1 parent 6959c8f commit 71f9d74
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/main/java/me/dio/controller/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ public record UserDto(
Long id,
String name,
AccountDto account,
CardDto card,
List<FeatureDto> features,
List<CardDto> cards,
List<NewsDto> news) {

public static UserDto fromModel(User model) {
var accountDto = AccountDto.fromModel(model.getAccount());
var cardDto = CardDto.fromModel(model.getCard());
var featuresDto = model.getFeatures().stream().map(FeatureDto::fromModel).collect(toList());
var cardsDto = model.getCards().stream().map(CardDto::fromModel).collect(toList());
var newsDto = model.getNews().stream().map(NewsDto::fromModel).collect(toList());

return new UserDto(model.getId(), model.getName(), accountDto, featuresDto, cardsDto, newsDto);
return new UserDto(model.getId(), model.getName(), accountDto, cardDto, featuresDto, newsDto);
}

public User toModel() {
User model = new User();
model.setId(this.id);
model.setName(this.name);
model.setAccount(this.account.toModel());
model.setCard(this.card.toModel());
model.setFeatures(this.features.stream().map(FeatureDto::toModel).collect(toList()));
model.setCards(this.cards.stream().map(CardDto::toModel).collect(toList()));
model.setNews(this.news.stream().map(NewsDto::toModel).collect(toList()));
return model;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/dio/domain/model/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity(name = "tb_accounts")
@Entity(name = "tb_account")
public class Account {

@Id
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/dio/domain/model/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity(name = "tb_cards")
@Entity(name = "tb_card")
public class Card {

@Id
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/dio/domain/model/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import jakarta.persistence.Entity;

@Entity(name = "tb_features")
@Entity(name = "tb_feature")
public class Feature extends BaseItem {

}
24 changes: 12 additions & 12 deletions src/main/java/me/dio/domain/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;

@Entity(name = "tb_users")
@Entity(name = "tb_user")
public class User {

@Id
Expand All @@ -22,13 +22,13 @@ public class User {

@OneToOne(cascade = CascadeType.ALL)
private Account account;

@OneToOne(cascade = CascadeType.ALL)
private Card card;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Feature> features;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Card> cards;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<News> news;

Expand Down Expand Up @@ -56,20 +56,20 @@ public void setAccount(Account account) {
this.account = account;
}

public List<Feature> getFeatures() {
return features;
public Card getCard() {
return card;
}

public void setFeatures(List<Feature> features) {
this.features = features;
public void setCard(Card card) {
this.card = card;
}

public List<Card> getCards() {
return cards;
public List<Feature> getFeatures() {
return features;
}

public void setCards(List<Card> cards) {
this.cards = cards;
public void setFeatures(List<Feature> features) {
this.features = features;
}

public List<News> getNews() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/dio/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public User update(Long id, User userToUpdate) {

dbUser.setName(userToUpdate.getName());
dbUser.setAccount(userToUpdate.getAccount());
dbUser.setCard(userToUpdate.getCard());
dbUser.setFeatures(userToUpdate.getFeatures());
dbUser.setCards(userToUpdate.getCards());
dbUser.setNews(userToUpdate.getNews());

return this.userRepository.save(dbUser);
Expand Down

0 comments on commit 71f9d74

Please sign in to comment.