LAB ASSIGNMENT 3
Name : Hitarth Singh Rajput
Roll No. : LCS2023040
Design the Spring Boot RESTs API for Users using ArrayList<>() in user services. Write appropriate Model , controller and service for following 3 APIs
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 + '\'' +
'}';
}
}
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);
}
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));
}
}
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);
}
}
Fetching All users after deleting the User with ID 3
:
3
has successfully been deleted.