-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAccountController.java
More file actions
60 lines (53 loc) · 2.85 KB
/
AccountController.java
File metadata and controls
60 lines (53 loc) · 2.85 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
package com.imjustdoom.pluginsite.controller;
import com.imjustdoom.pluginsite.config.exception.RestErrorCode;
import com.imjustdoom.pluginsite.config.exception.RestException;
import com.imjustdoom.pluginsite.dtos.in.account.UpdateAccountRequest;
import com.imjustdoom.pluginsite.dtos.out.account.AccountDto;
import com.imjustdoom.pluginsite.dtos.out.account.SelfAccountDto;
import com.imjustdoom.pluginsite.dtos.out.message.MessageGroupDto;
import com.imjustdoom.pluginsite.model.Account;
import com.imjustdoom.pluginsite.service.AccountService;
import com.imjustdoom.pluginsite.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/account")
@RequiredArgsConstructor
public class AccountController {
private final AccountService accountService;
private final MessageService messageService;
@GetMapping("/details")
@PreAuthorize("isAuthenticated()")
public SelfAccountDto getSelfAccountDetails(Account account) {
return SelfAccountDto.fromAccount(account);
}
@PatchMapping("/details")
@PreAuthorize("isAuthenticated()")
public SelfAccountDto updateAccountDetails(Account account, @RequestBody UpdateAccountRequest request,
@RequestParam(value = "profilePicture", required = false) MultipartFile file) throws RestException {
return SelfAccountDto.fromAccount(this.accountService.updateAccountDetails(account, request, file));
}
@GetMapping("/{id}/details")
public AccountDto getAccountDetails(@PathVariable int id) throws RestException {
return AccountDto.fromAccount(this.accountService.getAccount(id));
}
@GetMapping("/groups")
@PreAuthorize("isAuthenticated()")
public Page<MessageGroupDto> getMessageGroups(Account account,
@PageableDefault(size = 25) Pageable pageable) throws RestException {
if (pageable.getPageSize() > 50) throw new RestException(RestErrorCode.PAGE_SIZE_TOO_LARGE, "Page size is too large (%s > %s)", pageable.getPageSize(), 50);
return this.messageService.getGroups(account, pageable)
.map(MessageGroupDto::fromMessageGroup);
}
}