LAB ASSIGNMENT 3

Name : Hitarth Singh Rajput
Roll No. : LCS2023040


Assignment :

Design the Spring Boot RESTs API for Users using ArrayList<>() in user services.  Write appropriate Model , controller  and service for following 3 APIs

  1. Get All Users
  2. Get users by user name (not by userId)
  3. Delete the selected role by userId.

Dependencies :

  • Spring Web
  • Spring Data JPA
  • Spring Boot DevTools

Starter Config :

Pasted image 20240215201351.png

Building the REST API Project

Project Tree :

Pasted image 20240215212639.png

1. User Model : User.java

package org.example.rest.webservices.labassignment3.user;

import java.time.LocalDate;

public class User {
    private Integer id;
    private String name;
    private LocalDate birthDate;


    // Constructors
    public User(Integer id, String name, LocalDate birthDate) {
        this.id = id;
        this.name = name;
        this.birthDate = birthDate;
    }

    // Getters and Setters
    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;
    }

    // toString method
    @Override
    public String toString() {
        return "Users{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birthDate='" + birthDate + '\'' +
                '}';
    }
}

2. User Service Interface : UserService.java

package org.example.rest.webservices.labassignment3.user;
import java.util.List;

public interface UserService {
    List<User> getAllUsers();
    List<User> getUsersByUsername(String username);
    void deleteUserById(Integer userId);

}

3. Creating the Data Access Object : UserDaoService.java

To perform operations on something that is stored in the database we'll have to create a DAO i.e. a Data Access Object.

package org.example.rest.webservices.labassignment3.user;

import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class UserDaoService implements UserService {
    private static List<User> users = new ArrayList<>();
    private static int usersCount = 0;

    static {
        users.add(new User(++usersCount, "Hitarth", LocalDate.now()));
        users.add(new User(++usersCount, "Dev", LocalDate.now()));
        users.add(new User(++usersCount, "Vishal", LocalDate.now()));
        users.add(new User(++usersCount, "Vishal", LocalDate.now().minusYears(11)));
    }

    @Override
    public List<User> getAllUsers() {
        return users;
    }

    @Override
    public List<User> getUsersByUsername(String username) {
//        Predicate<? super User> predicate = user -> user.getName().equals(username);
        return users.stream().filter(user -> user.getName().equals(username)).collect(Collectors.toList());
    }

    @Override
    public void deleteUserById(Integer userId) {
        users.removeIf(user -> user.getId().equals(userId));
    }
}

4. Controller

To handle the HTTP requests, we'll create another file named UserController.java file.

package org.example.rest.webservices.labassignment3.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(path = "/users")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping(path = "/users/{username}")
    public List<User> getUserByUsername(@PathVariable String username) {
        return userService.getUsersByUsername(username);
    }

    @DeleteMapping(path = "/delete/{userId}")
    public void deleteById(@PathVariable Integer userId) {
        userService.deleteUserById(userId);
    }
}

Using Postman :

GET : Get All Users :

Pasted image 20240215221654.png

GET : Get Users By Username :

Pasted image 20240215221731.png

DELETE : Delete by user ID :

Pasted image 20240215222054.png
Fetching All users after deleting the User with ID 3 :
Pasted image 20240215222222.png

  • The user with ID 3 has successfully been deleted.