GithubHelp home page GithubHelp logo

spring-ca1's Introduction

Question 1

ApiController

package com.example.question_1.Controllers;

import java.util.ArrayList;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.question_1.Models.Address;

@RestController
public class ApiController {
    @GetMapping("/")
    public Iterable<Address> getAddresses() {
        // Create a list of addresses
        ArrayList<Address> addresses = new ArrayList<Address>();
        // Add some addresses to the list
        addresses.add(new Address("John", "123 Main Street", 12345));
        addresses.add(new Address("Jane", "456 Main Street", 12345));
        addresses.add(new Address("Joe", "789 Main Street", 12345));
        // Return the list of addresses
        return addresses;
    }
}

Models

package com.example.question_1.Models;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Address {
    private String employeeName;
    private String address;
    private int pincode;

    public Address() {
    }

    public Address(String employeeName, String address, int pincode) {
        this.employeeName = employeeName;
        this.address = address;
        this.pincode = pincode;
    }
}

Question 2

Controllers

package com.example.question_2.Controllers;

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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.question_2.Models.Student;
import com.example.question_2.Service.ApiService;

@RestController
public class ApiController {
    private ApiService apiService;

    @GetMapping("/")
    public Iterable<Student> getAllStudents() {
        return apiService.findAll();
    }

    @PostMapping("/")
    public Student addStudent(@RequestBody Student student) {
        return apiService.save(student);
    }

    @PutMapping("/{id}")
    public Student updateStudent(@PathVariable int id, @RequestBody Student student) {
        return apiService.save(student);
    }

    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable int id) {
        apiService.deleteById(id);
    }

}

Models

package com.example.question_2.Models;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "student")
public class Student {
    @Id
    @Column
    private int id;
    @Column
    private String studentName;
    @Column
    private String departmentName;
    @Column
    private String section;
    @Column
    private long mobile;
    @Column
    private String mailid;
    @Column
    private String address;

    public Student() {
    }

    public Student(int id, String studentName, String departmentName, String section, long mobile, String mailid,
            String address) {
        this.id = id;
        this.studentName = studentName;
        this.departmentName = departmentName;
        this.section = section;
        this.mobile = mobile;
        this.mailid = mailid;
        this.address = address;
    }
}

Service

package com.example.question_2.Service;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.question_2.Models.Student;

public interface ApiService extends JpaRepository<Student, Integer> {
}

Question 3

Controllers

package com.example.question_3.Controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.question_3.Models.Family;

interface FamilyRepository extends JpaRepository<Family, Integer> {
}

@RestController
public class ApiController {
    private final FamilyRepository familyRepository;

    public ApiController(FamilyRepository familyRepository) {
        this.familyRepository = familyRepository;
    }

    @GetMapping("/")
    public Iterable<Family> getFamily() {
        return familyRepository.findAll();
    }
}

Models

package com.example.question_3.Models;

import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "family")
public class Family {
    private String name;
    private int age;
    private String relationship;

    public Family() {
    }

    public Family(String name, int age, String relationship) {
        this.name = name;
        this.age = age;
        this.relationship = relationship;
    }
    
}

Question 4

Controllers

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

Question 5

Controllers

package com.example.question_2.Controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.question_2.Models.Student;
import com.example.question_2.Service.ApiService;

@RestController
public class ApiController {
    private ApiService apiService;

    @GetMapping("/")
    public Iterable<Student> getAllStudents() {
        return apiService.findAll();
    }

    @PostMapping("/")
    public Student addStudent(@RequestBody Student student) {
        return apiService.save(student);
    }
}

Models

package com.example.question_2.Models;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "student")
public class Student {
    @Id
    @Column
    private int id;
    @Column
    private String studentName;
    @Column
    private String departmentName;
    @Column
    private String section;
    @Column
    private long mobile;
    @Column
    private String mailid;
    @Column
    private String address;

    public Student() {
    }

    public Student(int id, String studentName, String departmentName, String section, long mobile, String mailid,
            String address) {
        this.id = id;
        this.studentName = studentName;
        this.departmentName = departmentName;
        this.section = section;
        this.mobile = mobile;
        this.mailid = mailid;
        this.address = address;
    }
}

Service

package com.example.question_2.Service;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.question_2.Models.Student;

public interface ApiService extends JpaRepository<Student, Integer> {
}

spring-ca1's People

Contributors

siddhesh-agarwal avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.