Skip to content

JPA Task Nico #3

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 Screenshot Test/DELETE Todo.png
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 Screenshot Test/DELETE User.png
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 Screenshot Test/GET Todo.png
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 Screenshot Test/GET User.png
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 Screenshot Test/POST Todo.png
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 Screenshot Test/POST User.png
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 Screenshot Test/PUT Todo.png
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 Screenshot Test/PUT User.png
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
Expand Up @@ -10,4 +10,4 @@ public class PostgresDemoApplication {
public static void main(String[] args) {
SpringApplication.run(PostgresDemoApplication.class, args);
}
}
}

This file was deleted.

This file was deleted.

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

import com.example.postgresdemo.exception.ResourceNotFoundException;
import com.example.postgresdemo.model.Todo;
import com.example.postgresdemo.repository.TodoRepository;
import com.example.postgresdemo.repository.UserRepository;
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 TodoController {

@Autowired
private TodoRepository todoRepository;

@Autowired
private UserRepository userRepository;

@GetMapping ("/users/{userId}/todos")
public List<Todo> getTodoByUserId(@PathVariable Long userId) {
return todoRepository.findByUserId(userId);
}

@PostMapping("/users/{userId}/todos")
public Todo addTodo(@PathVariable Long userId,
@Valid @RequestBody Todo todo) {
return userRepository.findById(userId)
.map(user -> {
todo.setUser(user);
return todoRepository.save(todo);
}).orElseThrow(() -> new ResourceNotFoundException("User not found with id" + userId));
}

@PutMapping ("users/{userId}/todos/{todoId}")
public Todo updateTodo (@PathVariable Long userId,
@PathVariable Long todoId,
@Valid @RequestBody Todo todosRequest
) {
if(!userRepository.existsById(userId)) {
throw new ResourceNotFoundException("User not found with id" + userId);
}

return todoRepository.findById(userId)
.map(todo -> {
todo.setTodos(todosRequest.getTodos());
return todoRepository.save(todo);
}).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id " + todoId));
}

@DeleteMapping("/users/{userId}/todo/{todoId}")
public ResponseEntity<?> deleteTodo(@PathVariable Long userId,
@PathVariable Long todoId) {
if (!todoRepository.existsById(userId)) {
throw new ResourceNotFoundException("User not found with id " + userId);
}

return todoRepository.findById(todoId)
.map(todo -> {
todoRepository.delete(todo);
return ResponseEntity.ok().build();
}).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id" + todoId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.postgresdemo.controller;


import com.example.postgresdemo.exception.ResourceNotFoundException;
import com.example.postgresdemo.model.User;
import com.example.postgresdemo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
public class UserController {

@Autowired
private UserRepository userRepository;

@GetMapping("/users")
public Page<User> getUsers(Pageable pageable) {
return userRepository.findAll(pageable);
}


@PostMapping("/users")//pakai http post, jadi berbeda dengan get walaupun sama-sema menggunakan /question
public User createUser(@Valid @RequestBody User user) {

return userRepository.save(user);
}

@PutMapping("/users/{userId}") //pull update pakai http put
public User updateUser(@PathVariable Long userId,
@Valid @RequestBody User userRequest) {
return userRepository.findById(userId)
.map(user -> {
user.setUsername(userRequest.getUsername());
user.setName(userRequest.getName());
return userRepository.save(user);
}).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId));
}


@DeleteMapping("/users/{userId}")
public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
return userRepository.findById(userId)
.map(user -> {
userRepository.delete(user);
return ResponseEntity.ok().build();
}).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + userId));
}
}
53 changes: 0 additions & 53 deletions src/main/java/com/example/postgresdemo/model/Answer.java

This file was deleted.

49 changes: 0 additions & 49 deletions src/main/java/com/example/postgresdemo/model/Question.java

This file was deleted.

Loading