Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defined the User controller and service #7

Merged
merged 1 commit into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@

package xyz.subho.clone.twitter.controller;

import java.util.List;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.subho.clone.twitter.model.UserResponse;
Expand All @@ -46,18 +53,49 @@ ResponseEntity<UserResponse> getUserByUserIdOrUserName(

UserResponse userResponse;

if (utility.isUUID(userNameOrUserId)) {
var uuid = utility.converStringToUUID(userNameOrUserId);
userResponse = new UserResponse(userService.getUserByUserId(uuid));
return new ResponseEntity<>(userResponse, HttpStatus.OK);
} else {
var username =
userNameOrUserId.startsWith("@") ? userNameOrUserId.substring(1) : userNameOrUserId;
if (userNameOrUserId.startsWith("@")) {
var username = userNameOrUserId.substring(1);
userResponse = new UserResponse(userService.getUserByUserName(username));
return new ResponseEntity<>(userResponse, HttpStatus.OK);
}
var userId = utility.converStringToUUID(userNameOrUserId);
userResponse = new UserResponse(userService.getUserByUserId(userId));
return new ResponseEntity<>(userResponse, HttpStatus.OK);
}

@PostMapping
ResponseEntity<UserResponse> createUser(@RequestBody UserResponse userResponse) {
UserResponse createdUser = new UserResponse(userService.addUser(userResponse));
return new ResponseEntity<>(createdUser, HttpStatus.OK);
}

// ResponseEntity<Boolean> makeNewUser ()
@PatchMapping
ResponseEntity<UserResponse> updateUser(@RequestBody UserResponse userResponse) {
UserResponse updatedUser = new UserResponse(userService.editUser(userResponse));
return new ResponseEntity<>(updatedUser, HttpStatus.OK);
}

@PutMapping("/{userId}/follow")
ResponseEntity<HttpStatus> addFollower(@PathVariable UUID userId) {
userService.addFollower(userId);
return new ResponseEntity<>(HttpStatus.OK);
}

@DeleteMapping("/{userId}/follow")
ResponseEntity<HttpStatus> removeFollower(@PathVariable("userId") UUID userId) {
userService.removeFollower(userId);
return new ResponseEntity<>(HttpStatus.OK);
}

@GetMapping("/{userId}/followers")
ResponseEntity<List<UserResponse>> getFollowers(@PathVariable("userId") UUID userId) {
List<UserResponse> followers = userService.getFollowers(userId);
return new ResponseEntity<>(followers, HttpStatus.OK);
}

@GetMapping("/{userId}/followings")
ResponseEntity<List<UserResponse>> getFollowings(@PathVariable("userId") UUID userId) {
List<UserResponse> followings = userService.getFollowings(userId);
return new ResponseEntity<>(followings, HttpStatus.OK);
}
}
14 changes: 14 additions & 0 deletions src/main/java/xyz/subho/clone/twitter/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@

package xyz.subho.clone.twitter.service;

import java.util.List;
import java.util.UUID;
import xyz.subho.clone.twitter.entity.User;
import xyz.subho.clone.twitter.model.UserResponse;

public interface UserService {

public User getUserByUserName(String username);

public User getUserByUserId(UUID userId);

public User addUser(UserResponse user);

public User editUser(UserResponse user);

public boolean addFollower(UUID followerId);

public boolean removeFollower(UUID followerId);

public List<UserResponse> getFollowers(UUID userId);

public List<UserResponse> getFollowings(UUID userId);
}