Skip to content

Todo Samuel #4

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 SS/CreateTodo.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 SS/CreateUser.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 SS/Get all todos of a User.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 SS/GetpaginatedQuestions.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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> getTodosByUserId(@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 todoRequest) {
if(!userRepository.existsById(userId)) {
throw new ResourceNotFoundException("User not found with id " + userId);
}

return todoRepository.findById(todoId)
.map(todo -> {
todo.setText(todoRequest.getText());
return todoRepository.save(todo);
}).orElseThrow(() -> new ResourceNotFoundException("Todo not found with id " + todoId));
}

@DeleteMapping("/users/{userId}/todos/{todoId}")
public ResponseEntity<?> deleteTodo(@PathVariable Long userId,
@PathVariable Long todoId) {
if(!userRepository.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,49 @@
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")
public User createUser(@Valid @RequestBody User user) {
return userRepository.save(user);
}

@PutMapping("/Users/{UserId}")
public User updateUser(@PathVariable Long userId,
@Valid @RequestBody User userRequest) {
return userRepository.findById(userId)
.map(user -> {
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));
}
}
49 changes: 0 additions & 49 deletions src/main/java/com/example/postgresdemo/model/Question.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import javax.persistence.*;

@Entity
@Table(name = "answers")
public class Answer extends AuditModel {
@Table(name = "todos")
public class Todo extends AuditModel {
@Id
@GeneratedValue(generator = "answer_generator")
@GeneratedValue(generator = "todo_generator")
@SequenceGenerator(
name = "answer_generator",
sequenceName = "answer_sequence",
name = "todo_generator",
sequenceName = "todo_sequence",
initialValue = 1000
)
private Long id;
Expand All @@ -22,10 +22,10 @@ public class Answer extends AuditModel {
private String text;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "question_id", nullable = false)
@JoinColumn(name = "user_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Question question;
private User user;

public Long getId() {
return id;
Expand All @@ -43,11 +43,11 @@ public void setText(String text) {
this.text = text;
}

public Question getQuestion() {
return question;
public User getUser() {
return user;
}

public void setQuestion(Question question) {
this.question = question;
public void setUser(User user) {
this.user = user;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/example/postgresdemo/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.postgresdemo.model;

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

@Entity
@Table(name = "Users")
public class User extends AuditModel {
@Id
@GeneratedValue(generator = "user_generator")
@SequenceGenerator(
name = "user_generator",
sequenceName = "user_sequence",
initialValue = 1000
)
private Long id;

@NotBlank
@Size(min = 3, max = 100)
private String name;

public Long getId() {
return id;
}

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

public String getName() {
return name;
}

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

import com.example.postgresdemo.model.Answer;
import com.example.postgresdemo.model.Todo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface AnswerRepository extends JpaRepository<Answer, Long> {
List<Answer> findByQuestionId(Long questionId);
}
public interface TodoRepository extends JpaRepository<Todo, Long> {
List<Todo> findByUserId(Long userId);
}
Loading