Skip to content

Commit

Permalink
Merge pull request #119 from kkn1125/kkn1125/be/diary
Browse files Browse the repository at this point in the history
Kkn1125/be/diary
  • Loading branch information
kkn1125 authored Aug 8, 2022
2 parents c39402b + cf0a0b1 commit 1f6ea71
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 16 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.narang.web.entity.Diary;

public interface DiaryRepositoryCustom {
public Boolean update(Diary diary);
public Diary update(Diary diary);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public class DiaryRepositoryCustomImpl implements DiaryRepositoryCustom {
}

@Override
public Boolean update(Diary diary) {
public Diary update(Diary diary) {
Diary foundDiary = diaryTemplate.findById(diary.getId(), Diary.class);
foundDiary.replaceIfNotNull(diary);
diaryTemplate.save(foundDiary, "diary");
return true;
return diaryTemplate.save(foundDiary, "diary");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.narang.web.entity.Like;

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

public interface LikeRepositoryCustom {
public Optional<Like> findByUid(String uid);

public Optional<Like> findByDid(String did);
public List<Like> findByDid(String did);

public Boolean deleteByUid(String uid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

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

public class LikeRepositoryCustomImpl implements LikeRepositoryCustom {
Expand All @@ -25,11 +26,11 @@ public Optional<Like> findByUid(String uid) {
}

@Override
public Optional<Like> findByDid(String did) {
public List<Like> findByDid(String did) {
Criteria cr = new Criteria("did").is(did);
Query q = new Query(cr);
Like foundLike = likeTemplate.findOne(q, Like.class);
return Optional.of(foundLike);
List<Like> foundLike = likeTemplate.find(q, Like.class);
return foundLike;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface UserRepositoryCustom {
public Optional<User> findByEmail(String email);

public Boolean update(User user);

public User removeProfileImageById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ public Boolean update(User user) {
userTemplate.save(foundUser, "user");
return true;
}

@Override
public User removeProfileImageById(String id) {
User foundedUser = userTemplate.findById(id, User.class);
foundedUser.setProfileImg("");
return userTemplate.save(foundedUser, "user");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public String insert(Diary diary) {
}

@PutMapping("/diary")
public Boolean update(Diary diary) {
public String update(Diary diary) {
return diaryService.update(diary);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public String findById(@PathVariable("id") String id) throws JsonProcessingExcep
@GetMapping("/like/did/{did}")
public String findByDid(@PathVariable("did") String did) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();

return mapper.writeValueAsString(likeService.findByDid(did));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,38 @@ public Map<String, Object> fileupload(MultipartFile multipartFile, String id, St
return m;
}

@PostMapping("/profile/fileupload")
public Map<String, Object> profileFileupload(MultipartFile multipartFile, String id, String hashName) {
File targetFile = new File("src/main/frontend/src/profiles/"
+ id
+ "/" + hashName);
try {
InputStream fileStream = multipartFile.getInputStream();
FileUtils.copyInputStreamToFile(fileStream, targetFile);
} catch (IOException e) {
FileUtils.deleteQuietly(targetFile);
e.printStackTrace();
}
Map<String, Object> m = new HashMap<>();
return m;
}

@PutMapping("/user")
public Boolean update(User user) {
return userService.update(user);
public String update(User user) throws JsonProcessingException {
userService.update(user);
return mapper(userService.findById(user.getId()));
}

@DeleteMapping("/user/{id}")
public Boolean delete(@PathVariable("id") String id) {
return userService.deleteById(id);
}

@DeleteMapping("/user/profile/{id}")
public Boolean removeUserProfile(@PathVariable("id") String id) {
return userService.removeProfileImageById(id);
}

}

// [UnAuthorized error 날 때] https://subbak2.com/11
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/narang/web/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public String insert(Diary diary) {
return newDiary.getId();
}

public Boolean update(Diary diary){
return diaryRepository.update(diary);
public String update(Diary diary){
Diary updatedDiary = diaryRepository.update(diary);
return updatedDiary.getId();
}

public Boolean deleteById(String id) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/narang/web/service/LikeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public Like findByUid(String uid) {
return likeRepository.findByUid(uid).orElseThrow();
}

public Like findByDid(String did) {
return likeRepository.findByDid(did).orElseThrow();
public List<Like> findByDid(String did) {
return likeRepository.findByDid(did);
}

public String insert(Like like){
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/narang/web/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import com.narang.web.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -107,6 +110,22 @@ public Boolean checkPassword(String password, String id) {
}
}

public Boolean removeProfileImageById(String id) {
File file = new File("src/main/frontend/src/profiles/"+id);
if(file.isDirectory()) {
File[] files = file.listFiles();
for(File f : files) {
if(f.delete()) {
System.out.println("파일을 성공적으로 삭제 했습니다.");
} else {
System.out.println("없는 파일 입니다.");
}
}
}
userRepository.removeProfileImageById(id);
return true;
}

public Boolean update(User user) {
return userRepository.update(user);
}
Expand Down

0 comments on commit 1f6ea71

Please sign in to comment.