post and get on user table

This commit is contained in:
Mikayla Dobson
2023-01-25 16:56:10 -06:00
parent 94e73b97b6
commit a6ed0a6f02
10 changed files with 168 additions and 38 deletions

View File

@@ -41,6 +41,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -1,4 +1,4 @@
package com.innocuoussymmetry.recipin.repositories;
package com.innocuoussymmetry.recipin.config;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;

View File

@@ -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();
}
}

View File

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

View File

@@ -3,9 +3,8 @@ package com.innocuoussymmetry.recipin.controllers;
import com.innocuoussymmetry.recipin.models.User;
import com.innocuoussymmetry.recipin.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.Optional;
@RestController
@@ -24,7 +23,7 @@ public class UserController {
}
@GetMapping("/{id}")
public User getUserById(Integer id) {
public User getUserById(@PathVariable Integer id) {
Optional<User> maybe = userRepository.findById(id);
if (maybe.isEmpty()) {
return null;
@@ -32,4 +31,22 @@ public class UserController {
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);
}
}

View File

@@ -2,6 +2,8 @@ package com.innocuoussymmetry.recipin.models;
import jakarta.persistence.*;
import java.util.List;
@Entity
@Table(name="recipe")
public class Recipe {
@Id
@@ -21,9 +23,6 @@ public class Recipe {
@Column(name="description")
private String description;
@Column(name="ingredientnames")
private List<String> ingredientNames;
public String getName() {
return this.name;
}
@@ -55,12 +54,4 @@ public class Recipe {
public void setDescription(String description) {
this.description = description;
}
public List<String> getIngredientNames() {
return this.ingredientNames;
}
public void setIngredientNames(List<String> ingredientNames) {
this.ingredientNames = ingredientNames;
}
}

View File

@@ -2,8 +2,11 @@ package com.innocuoussymmetry.recipin.models;
import jakarta.persistence.*;
import java.util.Date;
import java.util.Map;
@Entity
@Table(name="users")
@Table(name="appusers")
public class User {
@Id
@Column(name="id")
@@ -11,10 +14,10 @@ public class User {
private Integer id;
@Column(name="firstname")
private String firstName;
private String firstname;
@Column(name="lastname")
private String lastName;
private String lastname;
@Column(name="handle")
private String handle;
@@ -23,25 +26,63 @@ public class User {
private String email;
@Column(name="isadmin")
private boolean isAdmin;
private Boolean isadmin;
@Column(name="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() {
return firstName;
return firstname;
}
public void setFirstName(String name) {
this.firstName = name;
this.firstname = name;
}
public String getLastName() {
return lastName;
return lastname;
}
public void setLastName(String name) {
this.lastName = name;
this.lastname = name;
}
public String getHandle() {
@@ -60,12 +101,16 @@ public class User {
this.email = email;
}
public boolean getIsActive() { return active; }
public void setIsActive(boolean active) { this.active = active; }
public boolean getIsAdmin() {
return isAdmin;
return isadmin;
}
public void setIsAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
this.isadmin = isAdmin;
}
public String getPassword() {
@@ -75,4 +120,25 @@ public class User {
public void setPassword(String 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;
}
}

View File

@@ -1,21 +1,10 @@
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.File;
import java.io.FileReader;
import java.sql.*;
import java.awt.EventQueue;
import java.util.stream.Stream;
@Configuration
@PropertySource("classpath:/resources/app.properties")
public class DBSeed {
private String constring;
private Connection conn;

View File

@@ -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> { }

View File

@@ -2,6 +2,6 @@ package com.innocuoussymmetry.recipin.repositories;
import com.innocuoussymmetry.recipin.models.*;
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> { }