0% found this document useful (0 votes)
23 views

Eetest

Uploaded by

gamingmagix09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Eetest

Uploaded by

gamingmagix09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Varun Kamath

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;
}

public String getUsername() {


return username;
}

// Setters
public void setId(String id) {
this.id = id;
}

public void setUsername(String username) {


this.username = username;
}
}
- User Mongo Database

- 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;
}

public List<String> getIngredients() {


return ingredients;
}

public String getInstructions() {


return instructions;
}

public int getCookingTime() {


return cookingTime;
}

public String getDifficultyLevel() {


return difficultyLevel;
}

public double getAverageRating() {


return averageRating;
}

public double getTotalRating() {


return totalRating;
}

public int getUsersRated() {


return usersRated;
}

public int getRecipeId() {


return recipeId;
}

// Setters
public void setId(String id) {
this.id = id;
}

public void setTitle(String title) {


this.title = title;
}

public void setIngredients(List<String> ingredients) {


this.ingredients = ingredients;
}

public void setInstructions(String instructions) {


this.instructions = instructions;
}

public void setCookingTime(int cookingTime) {


this.cookingTime = cookingTime;
}

public void setDifficultyLevel(String difficultyLevel) {


this.difficultyLevel = difficultyLevel;
}

public void setAverageRating(double averageRating) {


this.averageRating = averageRating;
}

public void setTotalRating(double totalRating) {


this.totalRating = totalRating;
}

public void setUsersRated(int usersRated) {


this.usersRated = usersRated;
}

public void setRecipeId(int recipeId) {


this.recipeId = recipeId;
}
}
- Recipe Mongo Database

View:
- Fetching data with recipe id 2

- Fetching data with recipe id 4

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"));
}

List<Recipe> oprec = recrepo.findByRecipeId(recipeId);

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 (username.equals(null) || username == "") {


return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(CommonResponse.getErrorResponse(HttpStatus.BAD_REQUEST.value(), "Missing User
ID"));
}

List<User> userOpList = userRepo.findByUsername(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)));
}
}

You might also like