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

feature/advertisement_history #70

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import pl.simpleascoding.tutoringplatform.domain.user.User;

import javax.persistence.*;
import javax.validation.constraints.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.Instant;

@Data
@Entity
Expand All @@ -33,15 +36,19 @@ public class Advertisement {
@Size(min = 50, max = 255, message = "Description has to be between 50 and 255 characters")
private String description;

@NotNull
private Instant creationDate;
Eukon05 marked this conversation as resolved.
Show resolved Hide resolved

@NotNull
@Positive
private BigDecimal costPerHour;

public Advertisement(AdvertisementCategory category, User author, String title, String description, BigDecimal costPerHour) {
public Advertisement(AdvertisementCategory category, User author, String title, String description, Instant creationDate, BigDecimal costPerHour) {
this.category = category;
this.author = author;
this.title = title;
this.description = description;
this.costPerHour = costPerHour;
this.creationDate = creationDate;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pl.simpleascoding.tutoringplatform.dto;

import java.math.BigDecimal;
import java.time.Instant;

public record AdvertisementDTO(String category, String authorUsername, String title, String description,
Instant creationDate,
BigDecimal costPerHour) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import pl.simpleascoding.tutoringplatform.domain.advertisement.Advertisement;

public interface AdvertisementRepository extends JpaRepository<Advertisement, Long> {
Page<Advertisement> findAllByAuthor_Name(String username, Pageable pageable);
Page<Advertisement> findAllByAuthor_NameOrderByCreationDateDesc(String username, Pageable pageable);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can add @PageableDefault or @SortDefault annotation to your controller method instead of a long JPA method name, but it's a valid approach and you don't need to change it if you don't want to.
Info about these annotations

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pl.simpleascoding.tutoringplatform.service.user.UserService;

import javax.transaction.Transactional;
import java.util.Date;

@Service
@RequiredArgsConstructor
Expand All @@ -29,6 +30,8 @@ public class AdvertisementServiceImpl implements AdvertisementService {

private final UserService userService;

private final Date date = new Date();

@Override
public RscpDTO<AdvertisementDTO> createAdvertisement(CreateAdvertisementDTO requestDTO) {
User author = getAuthorEntity(requestDTO);
Expand All @@ -43,7 +46,7 @@ public RscpDTO<AdvertisementDTO> createAdvertisement(CreateAdvertisementDTO requ

@Override
public RscpDTO<Page<AdvertisementDTO>> getUsersAdvertisements(String username, Pageable pageable) {
return new RscpDTO<>(RscpStatus.OK, "Advertisement history found", advertisementRepository.findAllByAuthor_Name(username, pageable).map(advertisementModelMapper::mapAdvertisementEntityToAdvertisementDTO));
return new RscpDTO<>(RscpStatus.OK, "Advertisement history found", advertisementRepository.findAllByAuthor_NameOrderByCreationDateDesc(username, pageable).map(advertisementModelMapper::mapAdvertisementEntityToAdvertisementDTO));
}

private void isUserSignedAsTeacher(User author) {
Expand All @@ -53,7 +56,7 @@ private void isUserSignedAsTeacher(User author) {
}

private Advertisement createAdvertisementEntity(CreateAdvertisementDTO requestDTO, User author, AdvertisementCategory category) {
return new Advertisement(category, author, requestDTO.title(), requestDTO.description(), requestDTO.costPerHour());
return new Advertisement(category, author, requestDTO.title(), requestDTO.description(), date.toInstant(), requestDTO.costPerHour());
}

private AdvertisementCategory getAdvertisementCategory(CreateAdvertisementDTO requestDTO) {
Expand Down