Show ads of people you follow Spring JPA

谁说胖子不能爱 提交于 2021-01-29 06:47:07

问题


I have a User, an Ad, and a table Follows that show who follows who.

What I need to do is I need to implement a button that on clicked (and being loged in) regsiters that follow on the Follows table, and them you need to be able to show just de Ads ofthe people you follow (Currently there's only a view of ALL the ads, and now I need to make one that just shows the one that have been posted by the users I follow)

Any ideas of how can I make that possible?

AdRepository:

@Repository
public interface AdRepository extends JpaRepository<DefaultAdEntity, Integer> {

    Slice<DefaultAdEntity> findAllByOrderByDateDesc(Pageable pageRequest);

    Optional<DefaultAdEntity> findByTitle(String title);
}

UserRepository:

@Repository
public interface UserRepository extends JpaRepository<DefaultUserEntity, Integer> {

    Optional<DefaultUserEntity> findByLogin(String email);

}

AdService:

public interface AdService {

    public AdEntity add(final DefaultAdEntity entity, Integer userId)
            throws DontLoggedException, RepeatedTitleException;

    public DefaultAdEntity findById(final Integer identifier);

    public Block<AdImageObject> getAdList(int page, int size);

}

Userservice:

public interface UserService {
    
    public DefaultUserEntity login(String email, String password)
            throws UnregisterEmailException, IncorrectLoginException;

    public Iterable<DefaultUserEntity> getAllEntities();

    public Iterable<DefaultUserEntity> getEntities(final Pageable page);

    public UserEntity add(final DefaultUserEntity entity) throws AlreadyRegistedLoginException;

    public UserEntity findById(final Integer identifier);

    public void remove(final DefaultUserEntity entity);
}

DefaultAdService:

@Autowired
    public DefaultAdService(final AdRepository repository, UserRepository repository_, PictureRepository repository3) {
        super();
        userRepository = checkNotNull(repository_, "Received a null pointer as repository");
        adRepository = checkNotNull(repository, "Received a null pointer as repository");
        pictureRepository = checkNotNull(repository3, "Received a null pointer as repository");
    }

    @Override
    public AdEntity add(DefaultAdEntity entity, Integer userId) throws DontLoggedException, RepeatedTitleException {

        if (userId == null) {
            throw new DontLoggedException();
        }

        Optional<DefaultAdEntity> adEntityRepository = adRepository.findByTitle(entity.getTitle());

        if (!adEntityRepository.isEmpty()) {
            throw new RepeatedTitleException(entity.getTitle());
        }

        DefaultUserEntity user = userRepository.findById(userId).get();
        entity.setUser(user);
        entity.setDate(LocalDateTime.now());

        DefaultAdEntity adEntity = adRepository.save(entity);

        return adEntity;
    }

    @Override
    public final DefaultAdEntity findById(final Integer identifier) {
        final DefaultAdEntity adEntity;

        checkNotNull(identifier, "Received a null pointer as identifier");

        if (adRepository.existsById(identifier)) {
            adEntity = adRepository.getOne(identifier);
        } else {
            adEntity = new DefaultAdEntity();
        }

        return adEntity;
    }

    @Override
    public Block<AdImageObject> getAdList(int page, int size) {
        AdImageObject adImage;

        Slice<DefaultAdEntity> ads = adRepository.findAllByOrderByDateDesc(PageRequest.of(page, size));

        List<DefaultPictureEntity> pictures = pictureRepository.findAll();
        List<AdImageObject> ListAdImage = new ArrayList<AdImageObject>();

        for (DefaultAdEntity a : ads) {
            List<DefaultPictureEntity> picts = new ArrayList<DefaultPictureEntity>();
            for (DefaultPictureEntity pict : pictures) {

                // Buscamos en la lista de imagenes si el anuncio tiene imagen
                if (pict.getAd().getId() == a.getId()) {

                    // Asignamos la imagen (si la tiene)
                    picts.add(pict);
                }
            }
            adImage = new AdImageObject(picts, a);
            ListAdImage.add(adImage);
        }

        return new Block<>(ListAdImage, ads.hasNext());
    };

}

DefaultAdEntity:

@Entity(name = "DefaultAdEntity")
@Table(name = "ad")
public class DefaultAdEntity implements AdEntity {

    /**
     * 
     */
    private static final long serialVersionUID = -5167113468442767604L;

    /*
     * Ad's ID.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    private Integer id = -1;

    @Column(name = "title", nullable = false, unique = false)
    private String title;

    @Column(name = "description", nullable = false, unique = false)
    private String description;

    @Column(name = "date", nullable = false, unique = false)
    private LocalDateTime date;

    @Column(name = "price", nullable = true, unique = false)
    private int price;

    @ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity = DefaultUserEntity.class)
    @JoinColumn(name = "UserId")
    private UserEntity user;

    public DefaultAdEntity() {
        super();
    }
... getters, setters.....

DefautUserEntity:

@Entity(name = "DefaultUserEntity")
@Table(name = "user")
public class DefaultUserEntity implements UserEntity {

    /**
     * Serialization ID.
     */
    private static final long serialVersionUID = 1010299073916686941L;

    /**
     * User's ID.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    private Integer id = -1;

    /**
     * Login del usuario (email)
     */
    @Column(name = "login", nullable = false, unique = true)
    private String login;

    /**
     * Login del usuario (email)
     */
    @Column(name = "password", nullable = false, unique = false)
    private String password;

    /**
     * Nombre Usuario
     */
    @Column(name = "name", nullable = false, unique = false)
    private String name;

    /**
     * Primer apellido
     */
    @Column(name = "firstName", nullable = false, unique = false)
    private String firstName;

    /**
     * Segundo apellido
     */
    @Column(name = "lastName", nullable = false, unique = false)
    private String lastName;

    /**
     * Ciudad
     */
    @Column(name = "city", nullable = false, unique = false)
    private String city;

    public DefaultUserEntity() {
        super();
    }

来源:https://stackoverflow.com/questions/64697405/show-ads-of-people-you-follow-spring-jpa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!