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

Backend#229/contribution #230

Merged
merged 4 commits into from
May 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.Backend.shareNote.domain.Oraganization.DTOs.contributiondto;

import lombok.Data;

@Data
public class ContributionResultDTO {
private String userId;
private String nickname;
private Integer quizScore;
private Integer likeScore;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.Backend.shareNote.domain.Oraganization.controller;

import com.Backend.shareNote.domain.Oraganization.service.ContributionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api")
public class ContributeController {
private final ContributionService contributionService;
@GetMapping("/contribute/{organizationId}")
public ResponseEntity<?> getContribution(@PathVariable String organizationId){
return contributionService.getContribution(organizationId);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@Document(collection = "organizations")
@Builder
Expand Down Expand Up @@ -115,7 +112,7 @@ public static class Quiz {
@Getter
@Slf4j
public static class LikesInfo {
private Map<String, UserLike> userLikes = new HashMap<>();
private Map<String, Set<BlockLike>> userLikes = new HashMap<>();

/**
* μ’‹μ•„μš”λ₯Ό μΆ”κ°€ν•˜κ±°λ‚˜ μ·¨μ†Œν•©λ‹ˆλ‹€.
Expand All @@ -129,46 +126,52 @@ public Boolean addLike(String userUuid, String blockId, String likerUuid) {
if(userUuid.equals(likerUuid)) {
throw new SelfLikedException("자기 블둝에 μ’‹μ•„μš”λ₯Ό λˆ„λ₯Ό 수 μ—†μŠ΅λ‹ˆλ‹€.");
}
return userLikes.computeIfAbsent(userUuid, k -> new UserLike())
.addBlockLike(blockId, likerUuid);

BlockLike blockLike = new BlockLike(blockId, likerUuid);
userLikes.putIfAbsent(userUuid, new HashSet<>());
if(userLikes.get(userUuid).contains(blockLike)) {
log.info("이미 μ’‹μ•„μš”λ₯Ό λˆ„λ₯Έ μƒνƒœμž…λ‹ˆλ‹€.");
userLikes.get(userUuid).remove(blockLike);
return false;
}else {
userLikes.get(userUuid).add(blockLike);
return true;
}
}

// ν•„μš”ν•œ λ©”μ„œλ“œ μΆ”κ°€...
}
@Getter
public static class UserLike {
private Map<String, BlockLike> blockLikes = new HashMap<>();

public Boolean addBlockLike(String blockId, String likerUuid) {
return blockLikes.computeIfAbsent(blockId, k -> new BlockLike())
.addLiker(likerUuid);
// κΈ°μ—¬λ„μ—μ„œ μ’‹μ•„μš” 개수λ₯Ό κ°€μ Έμ˜€κΈ° μœ„ν•œ λ©”μ„œλ“œ
public int getLikeCount(String userUuid){
return userLikes.get(userUuid).size();
}

// ν•„μš”ν•œ λ©”μ„œλ“œ μΆ”κ°€...
}
@Getter
public static class BlockLike {
private List<String> likers = new ArrayList<>();

public Boolean addLiker(String likerUuid) {
// μ’‹μ•„μš” μ·¨μ†Œ 둜직 κ΅¬ν˜„ 및 μ’‹μ•„μš” μ—¬λΆ€ 확인
if(likers.contains(likerUuid)){
likers.remove(likerUuid);
return false;
}
else{
likers.add(likerUuid);
return true;
}
private String blockId;
private String likerUuid;
public BlockLike(String blockId, String likerUuid) {
this.blockId = blockId;
this.likerUuid = likerUuid;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BlockLike blockLike = (BlockLike) o;
return Objects.equals(blockId, blockLike.blockId) && Objects.equals(likerUuid, blockLike.likerUuid);
}

// ν•„μš”ν•œ λ©”μ„œλ“œ μΆ”κ°€...
@Override
public int hashCode() {
return Objects.hash(blockId, likerUuid);
}
}




public void addPageToNote(String noteId, Page newPage) {
for (Note note : this.notes) {
if (note.getId().equals(noteId)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.Backend.shareNote.domain.Oraganization.service;

import com.Backend.shareNote.domain.Oraganization.DTOs.contributiondto.ContributionResultDTO;
import com.Backend.shareNote.domain.Oraganization.entity.Organization;
import com.Backend.shareNote.domain.Oraganization.repository.OrganizationRepository;
import com.Backend.shareNote.domain.User.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class ContributionService {
private final OrganizationRepository organizationRepository;

private final UserRepository userRepository;

public ResponseEntity<?> getContribution(String organizationId) {
try {
// organization μ°ΎκΈ°
Organization organization = organizationRepository.findById(organizationId)
.orElseThrow(() -> new IllegalArgumentException("ν•΄λ‹Ήν•˜λŠ” organization이 μ—†μŠ΅λ‹ˆλ‹€."));


// μš°μ„  organization의 λ©€λ²„λ“€μ˜ μš°μ„  quiz 맞좘게 있으면 +1을 ν•˜μž
List<ContributionResultDTO> resultlist = new ArrayList<>();

HashMap<String, Integer> quizMap = new HashMap<>();
HashMap<String, Integer> likeMap = new HashMap<>();

// quizMap μ΄ˆκΈ°ν™”
organization.getMembers().forEach(member -> {
quizMap.put(member, 0);
});

// λ…ΈνŠΈ 쑰회 ν•˜λ©΄μ„œ ν€΄μ¦ˆμ˜ 맞좘 λͺ©λ‘μ„ λ’€μ§€λ©΄μ„œ quizMap μ™„μ„±ν•˜κΈ°
organization.getNotes().forEach(note -> {
//note의 like 정보λ₯Ό κ°–κ³  μžˆλŠ” 맡
Map<String, Set<Organization.BlockLike>> userLikes = note.getLikesInfo().getUserLikes();

for (String uuid :note.getLikesInfo().getUserLikes().keySet()) {
likeMap.put(uuid, likeMap.getOrDefault(uuid,0) + userLikes.get(uuid).size() );
}


note.getQuiz().forEach(quiz -> {
for (String key : quiz.getCorrectUser()) {
quizMap.put(key, quizMap.get(key) + 1);
}
// ν€΄μ¦ˆ μΆœμ œμžλ„ 1점 μΆ”κ°€
quizMap.put(quiz.getUserId(), quizMap.get(quiz.getUserId()) + 1);
});
});

organization.getMembers().forEach(member -> {
// λͺ¨λ“  μœ μ €μ— λŒ€ν•΄μ„œ λ‹‰λ„€μž„, id, quizScore, (likeScore)λ₯Ό λ„£μ–΄μ£Όμž
ContributionResultDTO contributionResultDTO = new ContributionResultDTO();
contributionResultDTO.setUserId(member);

// λ‹‰λ„€μž„ μΆ”κ°€ν•˜κΈ°
userRepository.findById(member).ifPresent(user -> {
contributionResultDTO.setNickname(user.getNickname());
});

// quiz 점수 μΆ”κ°€
contributionResultDTO.setQuizScore(quizMap.get(member));

// like 점수 μΆ”κ°€ (좔후에 μΆ”κ°€ μ˜ˆμ •)
contributionResultDTO.setLikeScore(likeMap.getOrDefault(member,0));

resultlist.add(contributionResultDTO);
});

return ResponseEntity.ok().body(resultlist);
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { localStorageCache } from "prosemirror-image-plugin";
import { Plugin } from "prosemirror-state";
import { v4 as uuidv4 } from "uuid";

Expand All @@ -22,7 +23,7 @@ export const generateBlockIdPlugin = (guidGenerator = uuidv4) => {
newGuid = guidGenerator();
} while (generatedIds.has(newGuid));
generatedIds.add(newGuid);
tr.setNodeMarkup(pos, undefined, {...node.attrs, 'data-guid': newGuid});
tr.setNodeMarkup(pos, undefined, {...node.attrs, 'data-guid': newGuid, 'data-writer': localStorage.getItem('userId')});
modified = true;
} else {
generatedIds.add(currentGuid);
Expand All @@ -41,7 +42,7 @@ export const generateBlockIdPlugin = (guidGenerator = uuidv4) => {
newGuid = guidGenerator();
} while (generatedIds.has(newGuid));
generatedIds.add(newGuid);
tr.setNodeMarkup(pos, undefined, {...node.attrs, guid: newGuid});
tr.setNodeMarkup(pos, undefined, {...node.attrs, guid: newGuid, writer: localStorage.getItem('userId')});
modified = true;
}
} else {
Expand All @@ -53,7 +54,7 @@ export const generateBlockIdPlugin = (guidGenerator = uuidv4) => {
newGuid = guidGenerator();
} while (generatedIds.has(newGuid));
generatedIds.add(newGuid);
tr.setNodeMarkup(pos, undefined, {...node.attrs, guid: newGuid});
tr.setNodeMarkup(pos, undefined, {...node.attrs, guid: newGuid, writer: localStorage.getItem('userId')});
modified = true;
} else {
generatedIds.add(currentGuid);
Expand Down
4 changes: 2 additions & 2 deletions nodejs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const databaseName = 'shareDB';

function createConnectionString(databaseName) {
logger.info(`@@@@@@@@@@@mongodb://root:1234@localhost:27017/${databaseName}?authSource=admin`);
return `mongodb://root:1234@localhost:27017/${databaseName}?authSource=admin`;
//return `mongodb://root:1234@mongodbService:27017/${databaseName}?authSource=admin`;
//return `mongodb://root:1234@localhost:27017/${databaseName}?authSource=admin`;
return `mongodb://root:1234@mongodbService:27017/${databaseName}?authSource=admin`;
}

const server = http.createServer((req, res) => {
Expand Down
Loading