Skip to content
Merged
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
2 changes: 1 addition & 1 deletion demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.3.9" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.8.2" level="project" />
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.5.4" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.9" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.32" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.9" level="project" />
</component>
</module>
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
<artifactId>spring-data-commons</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/example/demo/RecipeRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

public interface RecipeRepository extends CrudRepository<Recipe, Long> {

}
57 changes: 57 additions & 0 deletions src/main/java/com/example/demo/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.demo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue
Long id;
String name;
String password;
Integer postCount;

public User() {
}

public User(Long id, String name, String password, Integer postCount) {
this.id = id;
this.name = name;
this.password = password;
this.postCount = postCount;
}

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

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getPostCount() {
return postCount;
}

public void setPostCount(Integer postCount) {
this.postCount = postCount;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/example/demo/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userController")
public class UserController {

@Autowired
private UserService service;

@GetMapping(value = "/read/{id}")
public ResponseEntity<User> read(@PathVariable Long id){
return new ResponseEntity<>(service.read(id), HttpStatus.OK);
}

@GetMapping(value = "/read")
public ResponseEntity<List<User>> readAll(){
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

@PostMapping(value = "/create")
public ResponseEntity<User> create(@RequestBody User user){
return new ResponseEntity<>(service.create(user), HttpStatus.CREATED);
}

@PutMapping(value = "/update/{id}")
public ResponseEntity<User> update(@PathVariable Long id, @RequestBody User user){
return new ResponseEntity<>(service.update(id, user), HttpStatus.OK);
}

@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<User> delete(@PathVariable Long id){
return new ResponseEntity<>(service.delete(id), HttpStatus.OK);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/example/demo/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.demo;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

public interface UserRepository extends CrudRepository<User, Long> {
}
41 changes: 41 additions & 0 deletions src/main/java/com/example/demo/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
@Autowired
private UserRepository repository;

public User create(User user){ return repository.save(user); }

public User read(Long id){ return repository.findById(id).get(); }

public List<User> readAll(){
Iterable<User> userIterable = repository.findAll();
List<User> result = new ArrayList<>();
userIterable.forEach(result::add);
return result;
}

public User update(Long id, User newUser){
User userInDatabase = read(id);
userInDatabase.setName(newUser.getName());
userInDatabase.setPassword(newUser.getPassword());
userInDatabase.setPostCount(newUser.getPostCount());
return repository.save(userInDatabase);
}

public User delete(User user){
repository.delete(user);
return user;
}

public User delete(Long id){
return delete(read(id));
}
}
Binary file added target/classes/com/example/demo/User.class
Binary file not shown.
Binary file not shown.
Binary file added target/classes/com/example/demo/UserRepository.class
Binary file not shown.
Binary file added target/classes/com/example/demo/UserService.class
Binary file not shown.