Designing Social Media Application using Spring Boot

 Designing Social Media Application using Spring Boot Rest Api

1. Build a Rest Api For a social Media Application 

2. Key Resources:

  • Users
  • Posts

3. Key Details:

  • User: id ,name, birthdate 
  • Post: id, description
for we would create API Around the Users and Posts,
as part of API we would be perform operations like 
delete user , update user,  add new user
add post for specific user, update post for specific user, delete post for specific  user etc.

Request Methods for REST API
  • GET
  • PUT
  • POST
  • DELETE
  • PATCH
USER Rest API:
  • Retrieve All Users:
    • GET/users
  • Create User
    • POST/users
  • Retrieve One User
    • GET/users/{id} ==> /user/1
  • Delete User
    • DELETE /users/{id} ==> /user/1
  • POST REST API inside your User
    • Retrieve  All Posts of the specific User
      • GET/users/{id}/posts
    • Create a Post for a User
      • POST/users/{id}/posts
    • Retrieve Detail of a post
      • GET/users/{id}/posts/{post_id}
We can user Singular or Plurals while using Request URL
we will follow Plurals as it would much easier to understand 
(users, posts) instead of user and post

Create User Entity Bean and UserDAOService Bean Classes


User.Java

import java.time.LocalDate;

public class User {

private Integer id;

private String name;

private LocalDate birthDate;

public User(Integer id, String name, LocalDate birthDate) {

super();

this.id = id;

this.name = name;

this.birthDate = birthDate;

}


public Integer getId() {

return id;

}


public void setId(Integer id) {

this.id = id;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public LocalDate getBirthDate() {

return birthDate;

}


public void setBirthDate(LocalDate birthDate) {

this.birthDate = birthDate;

}


@Override

public String toString() {

return "User [id=" + id + ", name=" + name + ", birthDate=" + birthDate + "]";

}

}


UserDAOService.java

@Component

public class UserDaoService {

private static List<User> users = new ArrayList<>();

private static int usersCount = 0;

static {

users.add(new User(++usersCount, "Khan", LocalDate.now().minusYears(25)));

users.add(new User(++usersCount, "pathan", LocalDate.now().minusYears(50)));

users.add(new User(++usersCount, "tigher", LocalDate.now().minusYears(52)));

users.add(new User(++usersCount, "jim", LocalDate.now().minusYears(40)));

users.add(new User(++usersCount, "kabir", LocalDate.now().minusYears(35)));

}

public List<User> findAll(){

return users;

}

public User findOne(int id) {

Predicate<? super User> predicate =user -> user.getId().equals(id);

return users.stream().filter(predicate).findFirst().orElse(null);

}

public User save(User user) {

user.setId(++usersCount);

users.add(user);

return user;

}

}


Rest Controller for User UserResourceController.java:

for access your Entity and get Data from UserDAOService classes

@RestController

public class UserResourceController {

private UserDaoService userDaoService;

//user constructor injection

public UserResourceController(UserDaoService userDaoService) {

this.userDaoService = userDaoService;

}

//GET /users

@GetMapping("/users")

public List<User> retrieveAllUser() {

return userDaoService.findAll();

}

//GET /users

@GetMapping("/users/{id}")

public User retrieveUser(@PathVariable int id) throws UserPrincipalNotFoundException {

User user = userDaoService.findOne(id);

if(user == null) {

throw new UserNotFoundException("id:"+id);

}

return user;

}

//POST /users

@PostMapping("/users")

public ResponseEntity<User> addUser(@RequestBody User user) {

User savedUser = userDaoService.save(user);

URI location = ServletUriComponentsBuilder.fromCurrentRequest()

.path("/{id}")

.buildAndExpand(savedUser.getId())

.toUri();

// /user/7

return ResponseEntity.created(location).build();

}

}





Comments

Popular posts from this blog

Introduction of RESTful Web Service

Learn JPA and Hibernate

Implementing Dynamic Filtering for Rest API