Skip to content

Commit

Permalink
Some fixes about favourites
Browse files Browse the repository at this point in the history
  • Loading branch information
Fagorym committed Nov 7, 2023
1 parent bee4b43 commit 62bc2c6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/main/java/ru/nsu/fit/directors/userservice/model/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.proxy.HibernateProxy;

import java.util.Objects;

@Entity
@Table(name = "company")
Expand All @@ -16,4 +19,19 @@ public class Company {
@Id
private Long id;

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
Company company = (Company) o;
return getId() != null && Objects.equals(getId(), company.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.nsu.fit.directors.userservice.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class User implements UserDetails {
private String phoneNumber;
private String password;
private Long vkUserId;
@ManyToMany(fetch = FetchType.EAGER)
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
@JoinTable(
name = "favourite_companies",
joinColumns = @JoinColumn(name = "user_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import ru.nsu.fit.directors.userservice.model.Company;

import java.util.List;
import java.util.stream.Stream;

@Component
@RequiredArgsConstructor
Expand All @@ -22,7 +21,7 @@ public Company getCompanyById(Long establishmentId) {
);
}

public List<Company> getCompaniesByIds(Stream<Long> ids) {
public List<Company> getCompaniesByIds(List<Long> ids) {
return establishmentApi.syncListGetWithParams(
uriBuilder -> uriBuilder.path("/internal/establishment").queryParam("ids", ids).build(),
new ParameterizedTypeReference<>() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public void addToFavourites(Long establishmentId) {
@Transactional
public void deleteFromFavourites(Long establishmentId) {
User loggedUser = securityService.getLoggedInUser();
loggedUser.getFavourites().remove(new Company().setId(establishmentId));
loggedUser.getFavourites().remove(companyRepository.findById(establishmentId).orElseThrow());
userRepository.save(loggedUser);
}

@Override
@Transactional
public List<Company> getFavourites() {
User loggedUser = securityService.getLoggedInUser();
return companyService.getCompaniesByIds(loggedUser.getFavourites().stream().map(Company::getId));
return companyService.getCompaniesByIds(loggedUser.getFavourites().stream().map(Company::getId).toList());
}
}

0 comments on commit 62bc2c6

Please sign in to comment.