-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathChannelController.java
More file actions
152 lines (126 loc) · 5.59 KB
/
ChannelController.java
File metadata and controls
152 lines (126 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.sprint.mission.discodeit.controller;
import static org.springframework.http.HttpStatus.*;
import java.net.URI;
import java.util.List;
import java.util.UUID;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.sprint.mission.discodeit.domain.dto.CreatePrivateChannelDTO;
import com.sprint.mission.discodeit.domain.dto.CreatePublicChannelDTO;
import com.sprint.mission.discodeit.domain.dto.UpdateChannelDTO;
import com.sprint.mission.discodeit.domain.dto.channel.ChannelDto;
import com.sprint.mission.discodeit.domain.dto.channel.ChannelResponse;
import com.sprint.mission.discodeit.domain.request.CreatePrivateChannelRequest;
import com.sprint.mission.discodeit.domain.request.CreatePublicChannelRequest;
import com.sprint.mission.discodeit.domain.request.UpdatePublicChannelRequest;
import com.sprint.mission.discodeit.exception.ErrorResponse;
import com.sprint.mission.discodeit.mapper.ChannelMapper;
import com.sprint.mission.discodeit.service.ChannelService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/channels")
@Tag(name = "Channel", description = "Channel API")
@Slf4j
public class ChannelController {
private final ChannelService channelService;
private final ChannelMapper channelMapper;
@PostMapping("/public")
public ResponseEntity<ChannelResponse> createPublicChannel(
@RequestBody @Valid CreatePublicChannelRequest request) {
log.debug("Request to create PUBLIC channel started channelName={}", request.getName());
ChannelDto result = channelService.createPublic(CreatePublicChannelDTO.builder()
.name(request.getName())
.description(request.getDescription())
.build());
URI location = URI.create("api/channels");
log.debug("URI location={} in channelName={}", location, request.getName());
log.debug("PUBLIC channel created Request successfully done channelName={}", request.getName());
return ResponseEntity.created(location).body(channelMapper.toResponse(result));
}
@PostMapping("/private")
public ResponseEntity<ChannelResponse> createPrivateChannel(
@RequestBody @Valid CreatePrivateChannelRequest request) {
log.debug("Request to create PRIVATE channel started participantsIDs={}",
request.getParticipantIds().toString());
ChannelDto result = channelService.createPrivate(CreatePrivateChannelDTO.builder()
.userIds(request.getParticipantIds())
.build());
URI location = URI.create("api/channels");
log.debug("URI location={} in participantsIDs={}", location, request.getParticipantIds().toString());
log.debug("PRIVATE channel created Request successfully done channelName={}",
request.getParticipantIds().toString());
return ResponseEntity.created(location).body(channelMapper.toResponse(result));
}
@ApiResponses(value = {
@ApiResponse(
responseCode = "204",
description = "Channel이 성공적으로 삭제됨"
),
@ApiResponse(
responseCode = "404",
description = "Channel을 찾을 수 없음",
content = @Content(
schema = @Schema(implementation = ErrorResponse.class),
examples = {
@ExampleObject(
value = "{ \"status\": 404, \"errMessage\": \"Channel with id {channelId} not found\" }"
)
}
)
)
})
@DeleteMapping("{channelId}")
public ResponseEntity<Void> deleteChannel(
@Parameter(
description = "삭제할 Channel ID (UUID 형식)",
required = true
)
@PathVariable UUID channelId) {
log.debug("Request to update channel started channelID={}", channelId);
channelService.delete(channelId);
log.debug("channel update Request successfully done channelID={}", channelId);
return ResponseEntity.noContent().build();
}
@PatchMapping("/{channelId}")
public ResponseEntity<ChannelResponse> updatePublicChannel(
@Parameter(
description = "수정할 Channel ID"
)
@PathVariable UUID channelId,
@RequestBody @Valid UpdatePublicChannelRequest request) {
log.debug("Request to update channel started newChannelName={}", request.getNewName());
ChannelDto result = channelService.update(UpdateChannelDTO.builder()
.id(channelId)
.name(request.getNewName())
.description(request.getNewDescription())
.build());
log.debug("channel update Request successfully done newChannelName={}", request.getNewName());
return ResponseEntity.status(OK).body(channelMapper.toResponse(result));
}
@GetMapping
public ResponseEntity<List<ChannelResponse>> getAllByUserId(
@Parameter(description = "조회할 User ID")
@RequestParam UUID userId) {
List<ChannelDto> channels = channelService.readAllByUserId(userId);
List<ChannelResponse> body = channels.stream().map(channelMapper::toResponse).toList();
return ResponseEntity.ok(body);
}
}