| 1 | package edu.ucsb.cs156.happiercows.services; | |
| 2 | ||
| 3 | import java.time.LocalDateTime; | |
| 4 | import java.util.Optional; | |
| 5 | ||
| 6 | import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | import org.springframework.stereotype.Service; | |
| 8 | ||
| 9 | import edu.ucsb.cs156.happiercows.entities.Farmer; | |
| 10 | import edu.ucsb.cs156.happiercows.entities.FarmerActivity; | |
| 11 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 12 | import edu.ucsb.cs156.happiercows.entities.Student; | |
| 13 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 14 | import edu.ucsb.cs156.happiercows.repositories.FarmerActivityRepository; | |
| 15 | import edu.ucsb.cs156.happiercows.utilities.PacificTimeUtils; | |
| 16 | ||
| 17 | /** | |
| 18 | * Records FarmerActivity rows (play-page views, buy/sell actions) for | |
| 19 | * course-linked games whose farmer's email matches a student on that | |
| 20 | * course's roster. See issue #291. | |
| 21 | */ | |
| 22 | @Service | |
| 23 | public class FarmerActivityService { | |
| 24 | ||
| 25 | @Autowired | |
| 26 | private CourseAccessService courseAccessService; | |
| 27 | ||
| 28 | @Autowired | |
| 29 | private FarmerActivityRepository farmerActivityRepository; | |
| 30 | ||
| 31 | /** | |
| 32 | * Records a FarmerActivity row for the given user/game/farmer/activityType, | |
| 33 | * but only if the game is linked to a course and the user's email matches | |
| 34 | * a student on that course's roster. Silently does nothing otherwise: | |
| 35 | * tracking is opt-in-by-roster-membership, not an error condition when | |
| 36 | * it doesn't apply (e.g. a standalone game with no course, or a course | |
| 37 | * staff member rather than a student). | |
| 38 | */ | |
| 39 | public void recordActivityIfStudentMatch( | |
| 40 | User user, Game game, Farmer farmer, int activityType, int numCows) { | |
| 41 | Optional<Student> matchingStudent = | |
| 42 | courseAccessService.findMatchingStudentForCourseLinkedGame(user, game); | |
| 43 |
1
1. recordActivityIfStudentMatch : negated conditional → KILLED |
if (matchingStudent.isEmpty()) { |
| 44 | return; | |
| 45 | } | |
| 46 | ||
| 47 | FarmerActivity activity = FarmerActivity.builder() | |
| 48 | .farmer(farmer) | |
| 49 | .studentId(matchingStudent.get().getId()) | |
| 50 | .timestamp(LocalDateTime.now(PacificTimeUtils.ZONE)) | |
| 51 | .activityType(activityType) | |
| 52 | .numCows(numCows) | |
| 53 | .build(); | |
| 54 | ||
| 55 | farmerActivityRepository.save(activity); | |
| 56 | } | |
| 57 | } | |
Mutations | ||
| 43 |
1.1 |