Skip to content

Commit

Permalink
Addition of User controller and service (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeesou committed Jun 19, 2021
1 parent 274dac3 commit ebcad39
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
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);
}

1 comment on commit ebcad39

@ohbus
Copy link
Member

@ohbus ohbus commented on ebcad39 Jun 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeesou Please make the controllers public, completely missed it.

Please sign in to comment.