Skip to content

Commit

Permalink
Merge pull request #8 from GDSC-KNU/feat/channel
Browse files Browse the repository at this point in the history
채널 CRUD 구현 #4
  • Loading branch information
F-hiller authored May 4, 2024
2 parents deb5328 + bbd1a76 commit 7a46922
Show file tree
Hide file tree
Showing 25 changed files with 904 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ out/
.vscode/

### env ###
.env
.env
application-test.yml
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testAnnotationProcessor('org.projectlombok:lombok')
testImplementation('org.projectlombok:lombok')

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/gdsc/comunity/controller/ChannelController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package gdsc.comunity.controller;

import gdsc.comunity.annotation.UserId;
import gdsc.comunity.dto.channel.*;
import gdsc.comunity.service.channel.ChannelServiceImpl;
import gdsc.comunity.util.ListWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/channel")
@RequiredArgsConstructor
public class ChannelController {
private final ChannelServiceImpl channelServiceImpl;

@PostMapping
ResponseEntity<String> createChannel(@RequestBody ChannelCreateDto channelCreateDto, @UserId Long id) {
String channelName = channelCreateDto.getChannelName();
String nickname = channelCreateDto.getNickname();
channelServiceImpl.createChannel(id, channelName, nickname);
return new ResponseEntity<>("Channel created.", HttpStatus.CREATED);
}

@GetMapping("/{channelId}")
ResponseEntity<ChannelInfoDto> searchChannel(@PathVariable Long channelId, @UserId Long id) {
ChannelInfoDto channelInfoDto = channelServiceImpl.searchChannel(channelId);
return new ResponseEntity<>(channelInfoDto, HttpStatus.OK);
}

@GetMapping("/join/{channelId}")
ResponseEntity<ListWrapper<List<ChannelJoinRequestDto>>> searchJoinRequest(@PathVariable Long channelId, @UserId Long id) {
List<ChannelJoinRequestDto> userList = channelServiceImpl.searchJoinRequest(id, channelId);
return new ResponseEntity<>(new ListWrapper<>(userList), HttpStatus.OK);
}

@PostMapping("/join/{channelId}")
ResponseEntity<String> sendJoinRequest(@RequestBody String nickname, @PathVariable Long channelId, @UserId Long id) {
channelServiceImpl.sendJoinRequest(nickname, id, channelId);
return new ResponseEntity<>("Channel joined.", HttpStatus.OK);
}

@PutMapping("/join/{channelId}")
ResponseEntity<String> leaveChannel(@PathVariable Long channelId, @UserId Long id) {
channelServiceImpl.leaveChannel(id, channelId);
return new ResponseEntity<>("Channel left.", HttpStatus.OK);
}

@DeleteMapping("/join/{channelId}")
ResponseEntity<String> deleteChannel(@PathVariable Long channelId, @UserId Long id) {
channelServiceImpl.deleteChannel(id, channelId);
return new ResponseEntity<>("Channel deleted.", HttpStatus.OK);
}

@PutMapping("/approve")
ResponseEntity<String> approveJoinChannel(@RequestBody ApproveJoinChannelDto approveJoinChannelDto, @UserId Long userId) {
Long targetUserId = approveJoinChannelDto.getUserId();
Long channelId = approveJoinChannelDto.getChannelId();
channelServiceImpl.approveJoinChannel(userId, targetUserId, channelId);
return new ResponseEntity<>("Channel joined.", HttpStatus.OK);
}

@PutMapping("/nickname")
ResponseEntity<String> changeNickname(@RequestBody ChannelNicknameDto channelNicknameDto, @UserId Long id) {
String nickname = channelNicknameDto.getNickname();
Long channelId = channelNicknameDto.getChannelId();
channelServiceImpl.changeNickname(id, channelId, nickname);
return new ResponseEntity<>("Nickname changed.", HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gdsc.comunity.controller;

import gdsc.comunity.dto.GoogleUserInfo;
import gdsc.comunity.dto.JwtTokensDto;
import java.util.Map;
import lombok.RequiredArgsConstructor;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/gdsc/comunity/dto/channel/ApproveJoinChannelDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gdsc.comunity.dto.channel;

import lombok.Getter;

@Getter
public class ApproveJoinChannelDto {
private Long userId;
private Long channelId;

public ApproveJoinChannelDto(Long userId, Long channelId) {
this.userId = userId;
this.channelId = channelId;
}
}
14 changes: 14 additions & 0 deletions src/main/java/gdsc/comunity/dto/channel/ChannelCreateDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gdsc.comunity.dto.channel;

import lombok.Getter;

@Getter
public class ChannelCreateDto {
private String channelName;
private String nickname;

public ChannelCreateDto(String channelName, String nickname) {
this.channelName = channelName;
this.nickname = nickname;
}
}
45 changes: 45 additions & 0 deletions src/main/java/gdsc/comunity/dto/channel/ChannelInfoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gdsc.comunity.dto.channel;

import gdsc.comunity.entity.user.User;
import gdsc.comunity.entity.userchannel.UserChannel;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class ChannelInfoDto {

@Getter
public static class UserDto {
private Long id;
private String email;
private String nickname;
private String profileImageUrl;

public UserDto(String nickname, User user) {
this.id = user.getId();
this.email = user.getEmail();
this.nickname = nickname;
this.profileImageUrl = user.getProfileImageUrl();
}
}

private String channelName;
private String openDate;
private String managerNickname;
private List<UserDto> channelUsers;

public ChannelInfoDto(String channelName, String openDate, String managerNickname, List<UserChannel> userChannelList, List<User> userList) {
this.channelName = channelName;
this.openDate = openDate;
this.managerNickname = managerNickname;
this.channelUsers = new ArrayList<>();

for (UserChannel userChannel : userChannelList) {
for (User user : userList) {
channelUsers.add(new UserDto(userChannel.getNickname(), user));
}
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/gdsc/comunity/dto/channel/ChannelJoinRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gdsc.comunity.dto.channel;

import lombok.Getter;

@Getter
public class ChannelJoinRequestDto {
Long id;
Long userId;
Long channelId;
String nickname;

public ChannelJoinRequestDto(Long id, Long userId, Long channelId, String nickname) {
this.id = id;
this.userId = userId;
this.channelId = channelId;
this.nickname = nickname;
}
}
14 changes: 14 additions & 0 deletions src/main/java/gdsc/comunity/dto/channel/ChannelNicknameDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gdsc.comunity.dto.channel;

import lombok.Getter;

@Getter
public class ChannelNicknameDto {
private String nickname;
private Long channelId;

public ChannelNicknameDto(String nickname, Long channelId) {
this.nickname = nickname;
this.channelId = channelId;
}
}
8 changes: 6 additions & 2 deletions src/main/java/gdsc/comunity/dto/chat/Chatting.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import gdsc.comunity.entity.channel.Channel;
import gdsc.comunity.entity.chat.ChatLog;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;

@AllArgsConstructor
public class Chatting {
public final String message;
public final String senderNickname;
Expand All @@ -20,4 +18,10 @@ public ChatLog toEntity(Channel channel, Long senderId) {
.sendTime(time)
.build();
}

public Chatting(String message, String senderNickname, LocalDateTime time) {
this.message = message;
this.senderNickname = senderNickname;
this.time = time;
}
}
35 changes: 26 additions & 9 deletions src/main/java/gdsc/comunity/entity/channel/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@

import gdsc.comunity.entity.common.BaseTimeEntity;
import gdsc.comunity.entity.user.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import gdsc.comunity.entity.userchannel.UserChannel;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.HashSet;
import java.util.Set;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
Expand All @@ -40,9 +37,29 @@ public class Channel extends BaseTimeEntity {
@JoinColumn(name = "manager_id")
private User manager;

private String channelName;

@OneToMany(mappedBy = "channel", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<UserChannel> userChannels;

@Builder
private Channel(User manager) {
private Channel(User manager, String channelName) {
this.manager = manager;
this.channelName = channelName;
}

public void updateManager(User newManager) {
this.manager = newManager;
}

public void addUserChannel(UserChannel userChannel) {
if (userChannels == null) {
userChannels = new HashSet<>();
}
userChannels.add(userChannel);
}

public void removeUserChannel(UserChannel userChannel) {
userChannels.remove(userChannel);
}
}
37 changes: 37 additions & 0 deletions src/main/java/gdsc/comunity/entity/channel/ChannelJoinRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gdsc.comunity.entity.channel;

import gdsc.comunity.entity.common.BaseTimeEntity;
import gdsc.comunity.entity.user.User;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class ChannelJoinRequest extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "channel_id")
private Channel channel;

@Column(nullable = false)
String nickname;

@Builder
private ChannelJoinRequest(User user, Channel channel, String nickname){
this.user = user;
this.channel = channel;
this.nickname = nickname;
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package gdsc.comunity.entity.user;
package gdsc.comunity.entity.userchannel;

import gdsc.comunity.entity.channel.Channel;
import gdsc.comunity.entity.common.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import gdsc.comunity.entity.user.User;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -40,7 +34,8 @@ private UserChannel(String nickname, User user, Channel channel) {
this.user = user;
this.channel = channel;
}
}



public void updateNickname(String nickname) {
this.nickname = nickname;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gdsc.comunity.repository.channel;

import gdsc.comunity.entity.channel.ChannelJoinRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

@Repository
public interface ChannelJoinRequestRepository extends JpaRepository<ChannelJoinRequest, Long> {
Optional<ChannelJoinRequest> findByUserIdAndChannelId(Long targetUserId, Long userId);

List<ChannelJoinRequest> findAllByChannelId(Long channelId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import gdsc.comunity.entity.channel.Channel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ChannelRepository extends JpaRepository<Channel, Long> {
}
Loading

0 comments on commit 7a46922

Please sign in to comment.