Skip to content

Commit 3157b60

Browse files
committed
Add endpoints for fetching thumbnail IDs and unique video IDs by genres
1 parent bdce1d5 commit 3157b60

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

src/main/java/com/programming/videoService/controller/VideoController.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ public ResponseEntity<?> getVideoIdFromThumbnailId(@PathVariable String id) {
130130
return new ResponseEntity<>(videoService.getVideoIdFromThumbnailId(id), HttpStatus.OK);
131131
}
132132

133+
@GetMapping("/getThumbnailIdsByVideoId/{videoId}")
134+
public ResponseEntity<?> getThumbnailIdsByVideoId(@PathVariable String videoId) {
135+
try {
136+
List<String> thumbnailIds = videoService.getThumbnailIdsByVideoId(videoId);
137+
return new ResponseEntity<>(thumbnailIds, HttpStatus.OK);
138+
} catch (Exception e) {
139+
return new ResponseEntity<>("Error fetching thumbnail IDs: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
140+
}
141+
}
142+
143+
//hanle views
144+
145+
133146
@PutMapping("/updateViews/{id}")
134147
public ResponseEntity<?> updateViews(@PathVariable String id) {
135148
videoService.updateViews(id);
@@ -275,7 +288,31 @@ public ResponseEntity<?> getGenresByUserId(@PathVariable String userId) {
275288
return new ResponseEntity<>("Error fetching combined genres: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
276289
}
277290
}
291+
278292

293+
// Hanle Recommend
294+
@GetMapping("/getUniqueVideoIdsByGenres/{userId}")
295+
public ResponseEntity<?> getUniqueVideoIdsByGenres(@PathVariable String userId) {
296+
try {
297+
List<String> uniqueVideoIds = videoService.getUniqueVideoIdsByGenres(userId);
298+
return new ResponseEntity<>(uniqueVideoIds, HttpStatus.OK);
299+
} catch (Exception e) {
300+
return new ResponseEntity<>("Error fetching video IDs: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
301+
}
302+
}
303+
304+
@GetMapping("/getThumbnailsByUserGenres/{userId}")
305+
public ResponseEntity<?> getThumbnailsByUserGenres(@PathVariable String userId) {
306+
try {
307+
List<String> thumbnailIds = videoService.getThumbnailIdsByUserGenres(userId);
308+
return new ResponseEntity<>(thumbnailIds, HttpStatus.OK);
309+
} catch (Exception e) {
310+
return new ResponseEntity<>("Error fetching thumbnails: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
311+
}
312+
}
313+
314+
315+
279316

280317

281318
//handle Report

src/main/java/com/programming/videoService/service/VideoService.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@
3939
import java.util.ArrayList;
4040
import java.util.Collections;
4141
import java.util.HashMap;
42+
import java.util.HashSet;
4243
import java.util.LinkedHashMap;
44+
import java.util.LinkedHashSet;
4345
import java.util.List;
4446
import java.util.Map;
4547

4648
import org.slf4j.Logger;
4749
import org.slf4j.LoggerFactory;
4850
import org.springframework.data.domain.Sort;
4951
import java.util.Properties;
52+
import java.util.Set;
53+
5054
import org.apache.kafka.common.serialization.StringSerializer;
5155
import org.apache.kafka.common.serialization.StringDeserializer;
5256
import java.time.Duration;
@@ -192,6 +196,19 @@ public String getVideoIdFromThumbnailId(String thumbnailId) {
192196
return gridFSFile.getMetadata().get("videoId").toString();
193197
}
194198

199+
public List<String> getThumbnailIdsByVideoId(String videoId) {
200+
Query query = Query.query(Criteria.where("metadata.videoId").is(videoId));
201+
List<GridFSFile> gridFSFiles = template.find(query).into(new ArrayList<>());
202+
203+
List<String> thumbnailIds = new ArrayList<>();
204+
for (GridFSFile file : gridFSFiles) {
205+
thumbnailIds.add(file.getObjectId().toString());
206+
}
207+
208+
return thumbnailIds;
209+
}
210+
211+
195212
public VideoWithStream getVideoWithStream(String id) throws IOException {
196213
GridFSFile gridFSFile = template.findOne(new Query(Criteria.where("_id").is(id)));
197214
MDC.put("type", "videoservice");
@@ -203,6 +220,8 @@ public VideoWithStream getVideoWithStream(String id) throws IOException {
203220
return null;
204221
}
205222

223+
//Hanle view
224+
206225
public void updateViews(String id) {
207226
Query query = new Query(Criteria.where("_id").is(id));
208227
Update update = new Update().inc("views", 1);
@@ -353,7 +372,7 @@ public Map<String, Integer> getGenresByUserId(String userId) {
353372
// 1. Lấy danh sách thumbnailId từ History
354373
List<String> thumbnailIds = getHistoryByUserId(userId);
355374

356-
Map<String, Integer> genreCounts = new LinkedHashMap<>();
375+
Map<String, Integer> genreCounts = new HashMap<>();
357376

358377
// 2. Duyệt qua từng thumbnailId
359378
for (String thumbnailId : thumbnailIds) {
@@ -374,12 +393,64 @@ public Map<String, Integer> getGenresByUserId(String userId) {
374393
}
375394
}
376395

377-
return genreCounts;
396+
// 3. Sắp xếp genres theo số lượng giảm dần và giới hạn 12 genres
397+
return genreCounts.entrySet().stream()
398+
.sorted((entry1, entry2) -> Integer.compare(entry2.getValue(), entry1.getValue())) // Sắp xếp giảm dần theo giá trị
399+
.limit(12) // Lấy tối đa 12 genres
400+
.collect(LinkedHashMap::new,
401+
(map, entry) -> map.put(entry.getKey(), entry.getValue()),
402+
LinkedHashMap::putAll);
378403
}
404+
405+
//handle recommend
406+
public List<String> getUniqueVideoIdsByGenres(String userId) {
407+
// 1. Lấy danh sách genres và số lần xuất hiện từ API /getGenresByUserId/{userId}
408+
Map<String, Integer> genreCounts = getGenresByUserId(userId);
409+
410+
Set<String> uniqueVideoIds = new LinkedHashSet<>();
411+
412+
// 2. Tìm videoId theo từng genre
413+
for (String genre : genreCounts.keySet()) {
414+
Query query = Query.query(Criteria.where("genres").is(genre));
415+
List<DBObject> videoFiles = mongoTemplate.find(query, DBObject.class, "fs.files");
416+
417+
for (DBObject video : videoFiles) {
418+
String videoId = video.get("_id").toString();
419+
uniqueVideoIds.add(videoId);
420+
}
421+
}
422+
423+
// 3. Trả về danh sách videoId (duy nhất, không trùng lặp)
424+
return new ArrayList<>(uniqueVideoIds);
425+
}
426+
427+
public List<String> getThumbnailIdsByUserGenres(String userId) {
428+
// Bước 1: Lấy genres từ userId
429+
Map<String, Integer> genreCounts = getGenresByUserId(userId);
430+
431+
// Dùng Set để đảm bảo không có `thumbnailId` trùng lặp
432+
Set<String> uniqueThumbnailIds = new LinkedHashSet<>();
379433

434+
// Bước 2: Lấy danh sách videoId từ từng genre
435+
for (String genre : genreCounts.keySet()) {
436+
Query query = Query.query(Criteria.where("genres").is(genre));
437+
List<DBObject> videos = mongoTemplate.find(query, DBObject.class, "fs.files");
438+
for (DBObject video : videos) {
439+
String videoId = video.get("_id").toString();
440+
441+
// Bước 3: Lấy danh sách thumbnailId từ videoId
442+
List<String> thumbnailIds = getThumbnailIdsByVideoId(videoId);
443+
uniqueThumbnailIds.addAll(thumbnailIds);
444+
}
445+
}
446+
447+
// Trả về danh sách thumbnailId duy nhất
448+
return new ArrayList<>(uniqueThumbnailIds);
449+
}
380450

451+
381452

382-
//hanlde Report
453+
//hanlde Report
383454
public void uploadReport(String videoId, String msg, String userId) {
384455
Report report = new Report(videoId, msg, userId);
385456
mongoTemplate.save(report);

0 commit comments

Comments
 (0)