Создай и реализуй интерфейс QuestionDtoDao с методом getById, который принимает в параметрах questionId и authorizedUserId. Возвращай Optional
Не могу сообразить как правильно реализовать хотелось бы понять как это сделать конкретного решения не нужно просто хотя бы направление в котором искать решение.
@Repository
public class QuestionDtoDaoImpl implements QuestionDtoDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public Optional<QuestionDtoDao> getById(long questionId,long authorizedUserId){
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "вопросы")
public class QuestionDto {
@Parameter(description = "id вопроса")
private Long id;
@Schema(description = "заголовок вопроса")
private String title;
@Schema(description = "id автора")
private Long authorId;
@Schema(description = "имя автора")
private String authorName;
@Schema(description = "ссылка на изображение автора")
private String authorImage;
@Schema (description = "описание вопроса")
private String description;
@Schema(description = "количество просмотров")
private Long viewCount;
@Schema(description = "репутация автора")
private Long authorReputation;
@Schema (description = "количество ответов на вопрос")
private Long countAnswer;
@Schema(description = "рейтинг вопроса")
private Long countValuable;
@Schema(description = "дата создания вопроса")
private LocalDateTime persistDateTime;
@Schema(description = "дата последнего обновления")
private LocalDateTime lastUpdateDateTime;
@Schema(description = "кол-во голосов за вопрос")
private Long countVote;
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "question")
public class Question implements Serializable {
@Serial
private static final long serialVersionUID = 8765151549804433494L;
@Id
@GeneratedValue(generator = "Question_seq")
private Long id;
@Column
@NotNull
private String title;
@NotNull
@Column
@Type(type = "org.hibernate.type.TextType")
private String description;
@CreationTimestamp
@Column(name = "persist_date", updatable = false)
@Type(type = "org.hibernate.type.LocalDateTimeType")
private LocalDateTime persistDateTime;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;
@NotNull
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "question_has_tag",
joinColumns = @JoinColumn(name = "question_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
private List<Tag> tags = new ArrayList<>();
@Column(name = "last_redaction_date", nullable = false)
@Type(type = "org.hibernate.type.LocalDateTimeType")
@UpdateTimestamp
private LocalDateTime lastUpdateDateTime;
@Column(name = "is_deleted")
private Boolean isDeleted;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "question", orphanRemoval = true)
private List<Answer> answers = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "question", orphanRemoval = true)
private List<CommentQuestion> commentQuestions;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "question", orphanRemoval = true)
private List<UserFavoriteQuestion> userFavoriteQuestions;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "question", orphanRemoval = true)
private List<VoteQuestion> voteQuestions = new ArrayList<>();
@PrePersist
private void prePersistFunction() {
checkConstraints();
}
@PreUpdate
private void preUpdateFunction() {
checkConstraints();
}
private void checkConstraints() {
if (this.tags == null || this.tags.isEmpty()) {
throw new ConstrainException("Экземпляр Question должен иметь в поле tags хотя бы один элемент");
}
if (this.isDeleted == null) {
this.isDeleted = false;
}
try {
if (this.user.getId() <= 0) {
throw new EntityNotFoundException("User id must be > 0 on create or update.");
}
} catch (NullPointerException e) {
throw new EntityNotFoundException("User id must be not null on create.");
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Question)) return false;
Question question = (Question) o;
return Objects.equals(id, question.id) &&
Objects.equals(title, question.title) &&
Objects.equals(description, question.description) &&
Objects.equals(persistDateTime, question.persistDateTime) &&
Objects.equals(user, question.user) &&
Objects.equals(tags, question.tags) &&
Objects.equals(lastUpdateDateTime, question.lastUpdateDateTime) &&
Objects.equals(isDeleted, question.isDeleted);
}
@Override
public int hashCode() {
return Objects.hash(id, title, description, persistDateTime, user, tags, lastUpdateDateTime, isDeleted);
}
}