post and get on user table
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -41,6 +41,12 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.springfox</groupId>
|
||||||
|
<artifactId>springfox-boot-starter</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.innocuoussymmetry.recipin.repositories;
|
package com.innocuoussymmetry.recipin.config;
|
||||||
|
|
||||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.innocuoussymmetry.recipin.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import springfox.documentation.builders.PathSelectors;
|
||||||
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||||
|
import springfox.documentation.spring.web.plugins.Docket;
|
||||||
|
import springfox.documentation.spi.DocumentationType;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class SpringFoxConfig {
|
||||||
|
@Bean
|
||||||
|
public Docket api() {
|
||||||
|
return new Docket(DocumentationType.SWAGGER_2)
|
||||||
|
.select()
|
||||||
|
.apis(RequestHandlerSelectors.any())
|
||||||
|
.paths(PathSelectors.any())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.innocuoussymmetry.recipin.controllers;
|
||||||
|
|
||||||
|
import com.innocuoussymmetry.recipin.models.Recipe;
|
||||||
|
import com.innocuoussymmetry.recipin.repositories.RecipeRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/recipe")
|
||||||
|
public class RecipeController {
|
||||||
|
private final RecipeRepository recipeRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public RecipeController(RecipeRepository repository) {
|
||||||
|
this.recipeRepository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping()
|
||||||
|
public Iterable<Recipe> getAllRecipes() {
|
||||||
|
return this.recipeRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Recipe getRecipeById(@PathVariable Integer id) {
|
||||||
|
Optional<Recipe> recipe = this.recipeRepository.findById(id);
|
||||||
|
return recipe.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public Recipe addNewRecipe(@RequestBody Recipe recipe) {
|
||||||
|
return this.recipeRepository.save(recipe);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,9 +3,8 @@ package com.innocuoussymmetry.recipin.controllers;
|
|||||||
import com.innocuoussymmetry.recipin.models.User;
|
import com.innocuoussymmetry.recipin.models.User;
|
||||||
import com.innocuoussymmetry.recipin.repositories.UserRepository;
|
import com.innocuoussymmetry.recipin.repositories.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -24,7 +23,7 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public User getUserById(Integer id) {
|
public User getUserById(@PathVariable Integer id) {
|
||||||
Optional<User> maybe = userRepository.findById(id);
|
Optional<User> maybe = userRepository.findById(id);
|
||||||
if (maybe.isEmpty()) {
|
if (maybe.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
@@ -32,4 +31,22 @@ public class UserController {
|
|||||||
|
|
||||||
return maybe.get();
|
return maybe.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public User updateUserById(@PathVariable Integer id, @RequestBody Map<String, String> updates) {
|
||||||
|
Optional<User> maybe = userRepository.findById(id);
|
||||||
|
|
||||||
|
if (maybe.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
User updatedUser = maybe.get();
|
||||||
|
return new User(updates);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping()
|
||||||
|
public User addNewUser(@RequestBody Map<String, String> user) {
|
||||||
|
User newUser = new User(user);
|
||||||
|
return userRepository.save(newUser);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package com.innocuoussymmetry.recipin.models;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
@Table(name="recipe")
|
@Table(name="recipe")
|
||||||
public class Recipe {
|
public class Recipe {
|
||||||
@Id
|
@Id
|
||||||
@@ -21,9 +23,6 @@ public class Recipe {
|
|||||||
@Column(name="description")
|
@Column(name="description")
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@Column(name="ingredientnames")
|
|
||||||
private List<String> ingredientNames;
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
@@ -55,12 +54,4 @@ public class Recipe {
|
|||||||
public void setDescription(String description) {
|
public void setDescription(String description) {
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getIngredientNames() {
|
|
||||||
return this.ingredientNames;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIngredientNames(List<String> ingredientNames) {
|
|
||||||
this.ingredientNames = ingredientNames;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,11 @@ package com.innocuoussymmetry.recipin.models;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name="users")
|
@Table(name="appusers")
|
||||||
public class User {
|
public class User {
|
||||||
@Id
|
@Id
|
||||||
@Column(name="id")
|
@Column(name="id")
|
||||||
@@ -11,10 +14,10 @@ public class User {
|
|||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
@Column(name="firstname")
|
@Column(name="firstname")
|
||||||
private String firstName;
|
private String firstname;
|
||||||
|
|
||||||
@Column(name="lastname")
|
@Column(name="lastname")
|
||||||
private String lastName;
|
private String lastname;
|
||||||
|
|
||||||
@Column(name="handle")
|
@Column(name="handle")
|
||||||
private String handle;
|
private String handle;
|
||||||
@@ -23,25 +26,63 @@ public class User {
|
|||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
@Column(name="isadmin")
|
@Column(name="isadmin")
|
||||||
private boolean isAdmin;
|
private Boolean isadmin;
|
||||||
|
|
||||||
@Column(name="password")
|
@Column(name="password")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
@Column(name="active")
|
||||||
|
private Boolean active;
|
||||||
|
|
||||||
|
@Column(name="datecreated")
|
||||||
|
private Date datecreated;
|
||||||
|
|
||||||
|
@Column(name="datemodified")
|
||||||
|
private Date datemodified;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
this.datecreated = new Date();
|
||||||
|
this.datemodified = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(Map<String, String> user) {
|
||||||
|
this.firstname = user.get("firstname");
|
||||||
|
this.lastname = user.get("lastname");
|
||||||
|
this.email = user.get("email");
|
||||||
|
this.handle = user.get("handle");
|
||||||
|
this.isadmin = Boolean.getBoolean(user.get("isadmin"));
|
||||||
|
this.password = user.get("password");
|
||||||
|
this.active = Boolean.getBoolean(user.get("active"));
|
||||||
|
this.datecreated = (user.get("datecreated") != null) ? new Date(user.get("datecreated")) : new Date();
|
||||||
|
this.datemodified = (user.get("datemodified") != null) ? new Date(user.get("datemodified")) : new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(String firstname, String lastname, String email, String handle, Boolean isadmin, String password, Boolean active) {
|
||||||
|
this.firstname = firstname;
|
||||||
|
this.lastname = lastname;
|
||||||
|
this.email = email;
|
||||||
|
this.handle = handle;
|
||||||
|
this.isadmin = isadmin;
|
||||||
|
this.password = password;
|
||||||
|
this.active = active;
|
||||||
|
this.datecreated = new Date();
|
||||||
|
this.datemodified = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
return firstName;
|
return firstname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String name) {
|
public void setFirstName(String name) {
|
||||||
this.firstName = name;
|
this.firstname = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLastName() {
|
public String getLastName() {
|
||||||
return lastName;
|
return lastname;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLastName(String name) {
|
public void setLastName(String name) {
|
||||||
this.lastName = name;
|
this.lastname = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHandle() {
|
public String getHandle() {
|
||||||
@@ -60,12 +101,16 @@ public class User {
|
|||||||
this.email = email;
|
this.email = email;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getIsActive() { return active; }
|
||||||
|
|
||||||
|
public void setIsActive(boolean active) { this.active = active; }
|
||||||
|
|
||||||
public boolean getIsAdmin() {
|
public boolean getIsAdmin() {
|
||||||
return isAdmin;
|
return isadmin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setIsAdmin(boolean isAdmin) {
|
public void setIsAdmin(boolean isAdmin) {
|
||||||
this.isAdmin = isAdmin;
|
this.isadmin = isAdmin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
@@ -75,4 +120,25 @@ public class User {
|
|||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date getDatecreated() {
|
||||||
|
return datecreated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDatecreated(Date datecreated) {
|
||||||
|
this.datecreated = datecreated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDatemodified() {
|
||||||
|
return datemodified;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDatemodified(Date datemodified) {
|
||||||
|
this.datemodified = datemodified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Email: " + this.email + " firstname: " + this.firstname + " lastname: " + this.lastname + " handle: " + this.handle + " password: " + this.password;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,10 @@
|
|||||||
package com.innocuoussymmetry.recipin.repositories;
|
package com.innocuoussymmetry.recipin.repositories;
|
||||||
|
|
||||||
import org.hibernate.cfg.Environment;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.awt.EventQueue;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@PropertySource("classpath:/resources/app.properties")
|
|
||||||
public class DBSeed {
|
public class DBSeed {
|
||||||
private String constring;
|
private String constring;
|
||||||
private Connection conn;
|
private Connection conn;
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.innocuoussymmetry.recipin.repositories;
|
||||||
|
|
||||||
|
import com.innocuoussymmetry.recipin.models.Recipe;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
public interface RecipeRepository extends CrudRepository<Recipe, Integer> { }
|
||||||
@@ -2,6 +2,6 @@ package com.innocuoussymmetry.recipin.repositories;
|
|||||||
|
|
||||||
import com.innocuoussymmetry.recipin.models.*;
|
import com.innocuoussymmetry.recipin.models.*;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
public interface UserRepository extends CrudRepository<User, Integer> { }
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user