mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-03 12:34:40 +00:00
54 lines
1.7 KiB
Java
54 lines
1.7 KiB
Java
package com.openisle.controller;
|
|
|
|
import com.openisle.model.User;
|
|
import com.openisle.service.ImageUploader;
|
|
import com.openisle.service.UserService;
|
|
import lombok.Data;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/users")
|
|
@RequiredArgsConstructor
|
|
public class UserController {
|
|
private final UserService userService;
|
|
private final ImageUploader imageUploader;
|
|
|
|
@GetMapping("/me")
|
|
public ResponseEntity<UserDto> me(Authentication auth) {
|
|
User user = userService.findByUsername(auth.getName()).orElseThrow();
|
|
return ResponseEntity.ok(toDto(user));
|
|
}
|
|
|
|
@PostMapping("/me/avatar")
|
|
public ResponseEntity<?> uploadAvatar(@RequestParam("file") MultipartFile file,
|
|
Authentication auth) throws IOException {
|
|
String url = imageUploader.upload(file.getBytes(), file.getOriginalFilename());
|
|
userService.updateAvatar(auth.getName(), url);
|
|
return ResponseEntity.ok(Map.of("url", url));
|
|
}
|
|
|
|
private UserDto toDto(User user) {
|
|
UserDto dto = new UserDto();
|
|
dto.setId(user.getId());
|
|
dto.setUsername(user.getUsername());
|
|
dto.setEmail(user.getEmail());
|
|
dto.setAvatar(user.getAvatar());
|
|
return dto;
|
|
}
|
|
|
|
@Data
|
|
private static class UserDto {
|
|
private Long id;
|
|
private String username;
|
|
private String email;
|
|
private String avatar;
|
|
}
|
|
}
|