Skip to content

Commit 42e6b50

Browse files
committed
FullStack Spring Boot
1 parent 05237eb commit 42e6b50

File tree

6 files changed

+154
-3
lines changed

6 files changed

+154
-3
lines changed

Part-9.SpringBoot-React-Projects/Project-2.SpringBoot-React-ShoppingMall/fullstack/backend/src/main/java/com/urunov/controller/UserController.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
package com.urunov.controller;
22

3+
import com.urunov.payload.UserIdentityAvailability;
4+
import com.urunov.payload.UserSummary;
35
import com.urunov.repository.UserRepository;
6+
import com.urunov.security.CurrentUser;
7+
import com.urunov.security.UserPrincipal;
8+
import com.urunov.service.UserService;
9+
import com.urunov.utils.UserUtils;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.security.access.prepost.PreAuthorize;
14+
import org.springframework.web.bind.annotation.GetMapping;
415
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RequestParam;
517
import org.springframework.web.bind.annotation.RestController;
618

19+
720
/**
821
* Created by:
922
* User: hamdamboy
@@ -14,5 +27,31 @@
1427
@RequestMapping("/api")
1528
public class UserController {
1629

30+
@Autowired
1731
private UserRepository userRepository;
32+
33+
@Autowired
34+
private UserUtils userUtils;
35+
36+
@Autowired
37+
private UserService userService;
38+
39+
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
40+
41+
@GetMapping("/user/me")
42+
@PreAuthorize("hasRole('USER'")")
43+
public UserSummary getCurrentUser(@CurrentUser UserPrincipal currentUser)
44+
{
45+
UserSummary userSummary = new UserSummary(currentUser.getId(), currentUser.getUsername(),currentUser.getSurname(), currentUser.getName(), currentUser.getLastname(), currentUser.getCity());
46+
return userSummary;
47+
}
48+
49+
public UserIdentityAvailability checkUsernameAvailability(@RequestParam(value = "username") String username){
50+
Boolean isAvailable = !userRepository.existsByUsername(username);
51+
return new UserIdentityAvailability(isAvailable);
52+
}
53+
54+
55+
56+
1857
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.urunov.payload;
2+
3+
import lombok.Data;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
/**
8+
* Created by:
9+
* User: hamdamboy
10+
* Project: IntelliJ IDEA
11+
* Github: @urunov
12+
*/
13+
@Data
14+
@Getter
15+
@Setter
16+
public class UserIdentityAvailability {
17+
18+
private Boolean available;
19+
20+
public UserIdentityAvailability(Boolean available) {
21+
this.available = available;
22+
}
23+
24+
25+
}

Part-9.SpringBoot-React-Projects/Project-2.SpringBoot-React-ShoppingMall/fullstack/backend/src/main/java/com/urunov/payload/UserProfile.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public class UserProfile {
2323
private Long id;
2424

2525
@JsonIgnore
26-
private Instant avatarImgUrl;
26+
private Instant joinedAt;
27+
28+
@JsonIgnore
29+
private String avatarImgUrl;
2730

2831
private String surname;
2932
private String name;
@@ -34,6 +37,4 @@ public class UserProfile {
3437
private String password;
3538
private String city;
3639

37-
38-
3940
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.urunov.payload;
2+
3+
import lombok.*;
4+
5+
/**
6+
* Created by:
7+
* User: hamdamboy
8+
* Project: IntelliJ IDEA
9+
* Github: @urunov
10+
*/
11+
@Data
12+
@Getter
13+
@Setter
14+
@ToString
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class UserSummary {
18+
19+
private Long id;
20+
private String username;
21+
private String surname;
22+
private String name;
23+
private String lastname;
24+
private String city;
25+
26+
public UserSummary(Long id, String username, String name){
27+
this.id = id;
28+
this.username = username;
29+
this.name = name;
30+
}
31+
32+
33+
// public UserSummary(Long id, String username, String surname, String name, String lastname, String city) {
34+
// this.id = id;
35+
// this.username = username;
36+
// this.surname = surname;
37+
// this.name = name;
38+
// this.lastname = lastname;
39+
// this.city = city;
40+
// }
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.urunov.security;
2+
3+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
4+
5+
import java.lang.annotation.*;
6+
7+
/**
8+
* Created by:
9+
* User: hamdamboy
10+
* Project: IntelliJ IDEA
11+
* Github: @urunov
12+
*/
13+
@Target({ElementType.PARAMETER, ElementType.TYPE})
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Documented
16+
@AuthenticationPrincipal
17+
public @interface CurrentUser {
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.urunov.utils;
2+
3+
import com.urunov.model.User;
4+
import com.urunov.payload.UserProfile;
5+
import org.apache.tomcat.util.codec.binary.Base64;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.nio.charset.StandardCharsets;
9+
10+
/**
11+
* Created by:
12+
* User: hamdamboy
13+
* Project: IntelliJ IDEA
14+
* Github: @urunov
15+
*/
16+
@Component
17+
public class UserUtils
18+
{
19+
20+
public void initUserAvatar(UserProfile userProfile, User user){
21+
22+
if(user.getAvatar() !=null){
23+
userProfile.setAvatarImgUrl(new String(Base64.decodeBase64(user.getAvatar()), StandardCharsets.UTF_8));
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)