Skip to content

FUTURE - Tugas JPA - Nani Renova Hutagaol #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added picture postman result/table on pg web.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture postman result/todos_create_byUserId1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture postman result/todos_view_byUserId1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture postman result/users_create _after.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture postman result/users_delete_id2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture postman result/users_update_id2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture postman result/users_viewAll.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added picture postman result/users_view_id2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.postgresdemo.controller;

import com.example.postgresdemo.exception.ResourceNotFoundException;
import com.example.postgresdemo.model.Todos;
import com.example.postgresdemo.repository.TodosRepository;
import com.example.postgresdemo.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class TodosController {
@Autowired
private TodosRepository todosRepository;

@Autowired
private UsersRepository usersRepository;

@GetMapping("/users/{usersId}/todoList")
public List<Todos> getTodosByUsersId(@PathVariable Long usersId) {
return todosRepository.findByUsersId(usersId);
}

@PostMapping("/users/{usersId}/todoList")
public Todos createTodos(@PathVariable Long usersId, @Valid @RequestBody Todos todos) {
return usersRepository.findById(usersId)
.map(users -> {
todos.setUsers(users);
return todosRepository.save(todos);
}).orElseThrow(() -> new ResourceNotFoundException("Users not found with id " + usersId));
}

@PutMapping("/users/{usersId}/todoList/{todosId}")
public Todos updateTodos(@PathVariable Long usersId, @PathVariable Long todosId, @Valid @RequestBody Todos todosReq) {
if(!todosRepository.existsById(todosId)) {
throw new ResourceNotFoundException("Todos not found with id " + usersId);
}

return todosRepository.findById(todosId)
.map(todos -> {
todos.setTodo_list(todosReq.getTodo_list());
return todosRepository.save(todos);
}).orElseThrow(() -> new ResourceNotFoundException("Todos not found with id " + todosId));
}

@DeleteMapping("/users/{usersId}/todoList/{todosId}")
public ResponseEntity<?> deleteTodos(@PathVariable Long usersId, @PathVariable Long todosId) {
if(!todosRepository.existsById(usersId)) {
throw new ResourceNotFoundException("Todos not found with id " + todosId);
}

return todosRepository.findById(todosId)
.map(todos -> {
todosRepository.delete(todos);
return ResponseEntity.ok().build();
}).orElseThrow(() -> new ResourceNotFoundException("Todos not found with id " + todosId));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.postgresdemo.controller;

import com.example.postgresdemo.exception.ResourceNotFoundException;
import com.example.postgresdemo.model.Users;
import com.example.postgresdemo.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class UsersController {
@Autowired
private UsersRepository usersRepository;

@GetMapping("/users/viewAll")
public List<Users> getAllUser(){
return usersRepository.findAll();
}

@GetMapping("users/view/{usersId}")
public Users getUsersById(@PathVariable Long usersId){
return usersRepository.findById(usersId).map(users -> {return users;}).orElse(null);
}

@PostMapping("/users/create")
public Users createQuestion(@Valid @RequestBody Users users) {
return usersRepository.save(users);
}

@PutMapping("/users/update/{usersId}")
public Users updateUsers(@PathVariable Long usersId, @Valid @RequestBody Users usersReq) {
return usersRepository.findById(usersId)
.map(users -> {
users.setUsername(usersReq.getUsername());
users.setName(usersReq.getName());
return usersRepository.save(users);
}).orElseThrow(() -> new ResourceNotFoundException("Users not found with id " + usersId));
}

@DeleteMapping("/users/delete/{usersId}")
public ResponseEntity<?> deleteUsers(@PathVariable Long usersId) {
return usersRepository.findById(usersId)
.map(users -> {
usersRepository.delete(users);
return ResponseEntity.ok().build();
}).orElseThrow(() -> new ResourceNotFoundException("Users not found with id " + usersId));
}

}
53 changes: 53 additions & 0 deletions src/main/java/com/example/postgresdemo/model/Todos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.postgresdemo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;

@Entity
@Table(name = "todos")
public class Todos extends AuditModel {
@Id
@GeneratedValue(generator = "todos_generator")
@SequenceGenerator(
name = "todos_generator",
sequenceName = "todos_sequence",
initialValue = 1
)
private Long id;

@Column(columnDefinition = "text")
private String todo_list;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "users_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Users users;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTodo_list() {
return todo_list;
}

public void setTodo_list(String todo_list) {
this.todo_list = todo_list;
}

public Users getUsers() {
return users;
}

public void setUsers(Users users) {
this.users = users;
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/example/postgresdemo/model/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.postgresdemo.model;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@Entity
@Table(name = "users")
public class Users extends AuditModel{
@Id
@GeneratedValue(generator = "users_generator")
@SequenceGenerator(
name = "users_generator",
sequenceName = "users_sequence",
initialValue = 1
)
private Long id;

@NotBlank
@Size(min=3, max=30)
private String username;

@Column(columnDefinition = "text")
private String name;;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.postgresdemo.repository;

import com.example.postgresdemo.model.Todos;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface TodosRepository extends JpaRepository<Todos, Long> {
List<Todos> findByUsersId(Long usersId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.postgresdemo.repository;

import com.example.postgresdemo.model.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UsersRepository extends JpaRepository<Users, Long> {
}
9 changes: 6 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_demo
spring.datasource.username= rajeevkumarsingh
spring.datasource.password=
spring.datasource.url=jdbc:postgresql://stampy.db.elephantsql.com:5432/bdcccizh
spring.datasource.username= bdcccizh
spring.datasource.password= pSc6rYfsfzddYxli7J1ldgkdRGr8jS7z

spring.datasource.hikari.maximum-pool-size=3
spring.datasource.driver-class-name=org.postgresql.Driver

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
Expand Down