Skip to content

Commit

Permalink
added some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mehedikhan72 committed Aug 27, 2024
1 parent b14e543 commit d2c4108
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env.prototype
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ SPRING_PROFILES_ACTIVE=dev

RECOMBEE_PRIVATE_TOKEN = "replace_me"
RECOMBEE_DATABASE_ID = "replace_me"

GROQ_API_KEY = "replace_me"
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public ResponseEntity<?> createEnrollment(
return ResponseEntity.ok(service.createEnrollment(request, gigId));
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/get/{id}")
public ResponseEntity<?> getEnrollment(
@PathVariable Integer id
) {
return ResponseEntity.ok(service.findById(id));
}

@PutMapping("buyer-confirms/{enrollmentId}")
public String buyerConfirmsEnrollment(
@PathVariable Integer enrollmentId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.amplifiers.pathfinder.entity.enrollment;

import com.amplifiers.pathfinder.payment.PaymentService;
import com.amplifiers.pathfinder.entity.gig.Gig;
import com.amplifiers.pathfinder.entity.gig.GigRepository;
import com.amplifiers.pathfinder.entity.notification.NotificationService;
Expand All @@ -10,11 +9,13 @@
import com.amplifiers.pathfinder.exception.ResourceNotFoundException;
import com.amplifiers.pathfinder.exception.UnauthorizedException;
import com.amplifiers.pathfinder.exception.ValidationException;
import com.amplifiers.pathfinder.payment.PaymentService;
import com.amplifiers.pathfinder.utility.UserUtility;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -101,7 +102,7 @@ public String buyerConfirmsEnrollment(Integer enrollmentId) {
throw new UnauthorizedException("Only the buyer can confirm an enrollment.");
}

if(enrollment.isPaid() && enrollment.isBuyerConfirmed() && enrollment.getStartedAt() != null){
if (enrollment.isPaid() && enrollment.isBuyerConfirmed() && enrollment.getStartedAt() != null) {
throw new ValidationException("You have already paid and confirmed this enrollment.");
}

Expand Down Expand Up @@ -174,4 +175,14 @@ public boolean hasDeadlinePassed(Integer id) {
OffsetDateTime now = OffsetDateTime.now();
return now.isAfter(enrollment.getDeadline());
}

public Enrollment findById(Integer id) {
User user = userUtility.getCurrentUser();

// make sure the user is either the buyer or the seller as this is intended for the enrollment details view.
return enrollmentRepository.findById(id)
.filter(enrollment -> Objects.equals(user.getId(), enrollment.getBuyer().getId())
|| Objects.equals(user.getId(), enrollment.getGig().getSeller().getId()))
.orElseThrow(() -> new ResourceNotFoundException("Enrollment not found."));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.amplifiers.pathfinder.entity.report;

import com.amplifiers.pathfinder.entity.enrollment.Enrollment;
import com.amplifiers.pathfinder.entity.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -32,6 +33,10 @@ public class Report {
@JoinColumn(name = "reportedUserId")
private User reportedUser;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "enrollmentId")
private Enrollment enrollment;

private boolean resolved;
private String resolvedBy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
public class ReportCreateRequest {
private String text;
private Integer reportedUserId;
private Integer enrollmentId;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.amplifiers.pathfinder.entity.report;

import com.amplifiers.pathfinder.entity.enrollment.Enrollment;
import com.amplifiers.pathfinder.entity.enrollment.EnrollmentRepository;
import com.amplifiers.pathfinder.entity.user.Role;
import com.amplifiers.pathfinder.entity.user.User;
import com.amplifiers.pathfinder.entity.user.UserRepository;
Expand All @@ -18,18 +20,23 @@ public class ReportService {
private final ReportRepository reportRepository;
private final UserRepository userRepository;
private final UserUtility userUtility;
private final EnrollmentRepository enrollmentRepository;

public Report createReport(ReportCreateRequest reportCreateRequest) {

User reporter = userUtility.getCurrentUser();
User reportedUser = userRepository.findById(reportCreateRequest.getReportedUserId())
.orElseThrow(() -> new ResourceNotFoundException("User not found"));

Enrollment enrollment = enrollmentRepository.findById(reportCreateRequest.getEnrollmentId())
.orElseThrow(() -> new ResourceNotFoundException("Enrollment not found"));

var report = Report.builder()
.text(reportCreateRequest.getText())
.reporter(reporter)
.reportedUser(reportedUser)
.createdAt(OffsetDateTime.now())
.enrollment(enrollment)
.resolved(false)
.resolvedBy(null)
.resolvedAt(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public ResponseEntity<?> findRunningSessionByEnrollmentId(
) {
return ResponseEntity.ok(service.findRunningSessionByEnrollmentId(enrollmentId));
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/all/enrollment/{enrollmentId}")
public ResponseEntity<?> findAllByEnrollmentId(
@PathVariable Integer enrollmentId
) {
return ResponseEntity.ok(service.findAllByEnrollmentId(enrollmentId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface SessionRepository extends JpaRepository<Session, Integer> {
Expand All @@ -11,4 +12,5 @@ public interface SessionRepository extends JpaRepository<Session, Integer> {
where s.enrollment.id = :enrollmentId and s.completed = false and s.cancelled = false
""")
Optional<Session> findRunningSessionByEnrollmentId(Integer enrollmentId);
List<Session> findAllByEnrollmentId(Integer enrollmentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.amplifiers.pathfinder.entity.notification.NotificationService;
import com.amplifiers.pathfinder.entity.notification.NotificationType;
import com.amplifiers.pathfinder.entity.user.User;
import com.amplifiers.pathfinder.entity.user.UserRepository;
import com.amplifiers.pathfinder.exception.ResourceNotFoundException;
import com.amplifiers.pathfinder.exception.UnauthorizedException;
import com.amplifiers.pathfinder.exception.ValidationException;
Expand All @@ -14,6 +13,7 @@
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -22,7 +22,6 @@
public class SessionService {
private final SessionRepository sessionRepository;
private final EnrollmentRepository enrollmentRepository;
private final UserRepository userRepository;
private final UserUtility userUtility;
private final NotificationService notificationService;

Expand Down Expand Up @@ -154,7 +153,7 @@ public Session completeSession(Integer sessionId) {
Enrollment enrollment = session.getEnrollment();
enrollment.setNumSessionsCompleted(enrollment.getNumSessionsCompleted() + 1);

if(Objects.equals(enrollment.getNumSessions(), enrollment.getNumSessionsCompleted())) {
if (Objects.equals(enrollment.getNumSessions(), enrollment.getNumSessionsCompleted())) {
enrollment.setCompletedAt(java.time.OffsetDateTime.now());
}
enrollmentRepository.save(enrollment);
Expand Down Expand Up @@ -205,7 +204,24 @@ public String cancelSession(SessionCancelRequest request, Integer sessionId) {
return "Session cancelled.";
}

public boolean userPartOfEnrollment(Integer userId, Integer enrollmentId) {
Enrollment enrollment = enrollmentRepository.findById(enrollmentId).orElseThrow(() -> new ResourceNotFoundException("Enrollment not found."));
return Objects.equals(enrollment.getBuyer().getId(), userId) || Objects.equals(enrollment.getGig().getSeller().getId(), userId);
}

public Optional<Session> findRunningSessionByEnrollmentId(Integer EnrollmentId) {
return sessionRepository.findRunningSessionByEnrollmentId(EnrollmentId);
User user = userUtility.getCurrentUser();
if(userPartOfEnrollment(user.getId(), EnrollmentId)) {
return sessionRepository.findRunningSessionByEnrollmentId(EnrollmentId);
}
throw new UnauthorizedException("You are not part of this enrollment.");
}

public List<Session> findAllByEnrollmentId(Integer enrollmentId) {
User user = userUtility.getCurrentUser();
if(userPartOfEnrollment(user.getId(), enrollmentId)) {
return sessionRepository.findAllByEnrollmentId(enrollmentId);
}
throw new UnauthorizedException("You are not part of this enrollment.");
}
}

0 comments on commit d2c4108

Please sign in to comment.