Eetest
Eetest
SRN: PES2UG21CS594
Section: 6-J
Subject: Object Oriented Analysis and
Design using Java
Self Learning Hands-on Assignment:
MVC Framework
Model:
- User Model
package com.example.demo;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "user")
public class User {
private String id;
private String username;
// Constructors
public User() {
}
// Getters
public String getId() {
return id;
}
// Setters
public void setId(String id) {
this.id = id;
}
- Recipe Model
package com.example.demo;
import java.util.List;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "recipe")
public class Recipe {
private String id;
private String title;
private List<String> ingredients;
private String instructions;
private int cookingTime;
private String difficultyLevel;
private double averageRating;
private double totalRating;
private int usersRated;
private int recipeId;
Recipe() {
}
// Getters
public String getId() {
return id;
}
public String getTitle() {
return title;
}
// Setters
public void setId(String id) {
this.id = id;
}
View:
- Fetching data with recipe id 2
Controller:
Recipe Controller:
package com.example.demo;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RecipeController {
@Autowired
private RecipeRepository recrepo;
@GetMapping("/recipes")
@CrossOrigin("http://localhost:3000")
public ResponseEntity<Map<String, Object>> getAllProjects() {
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.getSuccessResponse(HttpStatus.OK.value(), "SUCCESS",
recrepo.findAll()));
}
@GetMapping("/recipesById")
@CrossOrigin("http://localhost:3000")
public ResponseEntity<Map<String, Object>> getProjectById(@RequestParam int recipeId) {
if (recipeId == 0) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(CommonResponse.getErrorResponse(HttpStatus.BAD_REQUEST.value(), "Missing Project
ID"));
}
if (oprec.isEmpty()) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(CommonResponse.getErrorResponse(HttpStatus.NOT_FOUND.value(), "Project not
found"));
}
return ResponseEntity.status(HttpStatus.OK)
.body(CommonResponse.getSuccessResponse(HttpStatus.OK.value(), "SUCCESS", oprec));
}
}
User Controller:
package com.example.demo;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserRepository userRepo;
@GetMapping("/getUserByUsername")
@CrossOrigin("http://localhost:3000")
public ResponseEntity<Map<String, Object>> getUserByUsername(@RequestParam String username) {
if (userOpList.isEmpty()) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(CommonResponse.getErrorResponse(HttpStatus.NOT_FOUND.value(), "User not found"));
}
return ResponseEntity.status(HttpStatus.OK)
.body(CommonResponse.getSuccessResponse(HttpStatus.OK.value(), "Success", userOpList.get(0)));
}
}