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 all commits
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
@@ -1,13 +1,12 @@
package pl.simpleascoding.tutoringplatform.api;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import pl.simpleascoding.tutoringplatform.dto.AdvertisementDTO;
import pl.simpleascoding.tutoringplatform.dto.CreateAdvertisementDTO;
import pl.simpleascoding.tutoringplatform.dto.RscpDTO;
Expand All @@ -34,4 +33,19 @@ public ResponseEntity<AdvertisementDTO> createAdvertisement(@RequestBody CreateA

return new ResponseEntity<>(advDTO, headers, httpStatus);
}

@GetMapping("/history/{username}")
public ResponseEntity<Page<AdvertisementDTO>> getUsersAdvertisements(@PathVariable String username, Pageable pageable) {
RscpDTO<Page<AdvertisementDTO>> rscpDTO = advertisementService.getUsersAdvertisements(username, pageable);
Page<AdvertisementDTO> responseBody = rscpDTO.body();

//Message
HttpHeaders headers = new HttpHeaders();
headers.add("message", rscpDTO.message());

//Status
HttpStatus httpStatus = HttpStatus.resolve(rscpDTO.status().value());

return new ResponseEntity<>(responseBody, headers, httpStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
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,6 +37,10 @@ public class Advertisement {
@Size(min = 50, max = 255, message = "Description has to be between 50 and 255 characters")
private String description;

@CreationTimestamp
@Column(name = "creationDate")
private Instant creationDate;
Eukon05 marked this conversation as resolved.
Show resolved Hide resolved

@NotNull
@Positive
private BigDecimal costPerHour;
Expand Down
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
@@ -1,7 +1,10 @@
package pl.simpleascoding.tutoringplatform.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import pl.simpleascoding.tutoringplatform.domain.advertisement.Advertisement;

public interface AdvertisementRepository extends JpaRepository<Advertisement, Long> {
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
@@ -1,11 +1,14 @@
package pl.simpleascoding.tutoringplatform.service.advertisement;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import pl.simpleascoding.tutoringplatform.dto.AdvertisementDTO;
import pl.simpleascoding.tutoringplatform.dto.CreateAdvertisementDTO;
import pl.simpleascoding.tutoringplatform.dto.RscpDTO;

public interface AdvertisementService {

RscpDTO<AdvertisementDTO> createAdvertisement(CreateAdvertisementDTO requestDTO);

RscpDTO<Page<AdvertisementDTO>> getUsersAdvertisements(String username, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pl.simpleascoding.tutoringplatform.service.advertisement;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import pl.simpleascoding.tutoringplatform.domain.advertisement.Advertisement;
import pl.simpleascoding.tutoringplatform.domain.advertisement.AdvertisementCategory;
Expand All @@ -15,6 +17,7 @@
import pl.simpleascoding.tutoringplatform.service.user.UserService;

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

@Service
@RequiredArgsConstructor
Expand All @@ -33,12 +36,17 @@ public RscpDTO<AdvertisementDTO> createAdvertisement(CreateAdvertisementDTO requ
isUserSignedAsTeacher(author);
AdvertisementCategory category = getAdvertisementCategory(requestDTO);
Advertisement advertisement = createAdvertisementEntity(requestDTO, author, category);
AdvertisementDTO advertisementDTO = advertisementModelMapper.mapAdvertisementEntityToAdvertisementDTO(advertisement);
advertisementRepository.save(advertisement);
AdvertisementDTO advertisementDTO = advertisementModelMapper.mapAdvertisementEntityToAdvertisementDTO(advertisement);

return new RscpDTO<>(RscpStatus.CREATED, "Advertisement created", advertisementDTO);
}

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

private void isUserSignedAsTeacher(User author) {
if (!author.getRoles().contains(RoleType.TEACHER)) {
throw new UserIsNotATeacherException();
Expand Down