modified config, database seed connects to PG, imported sql scripts from previous version

This commit is contained in:
Mikayla Dobson
2023-01-25 14:40:32 -06:00
parent 3c988c9897
commit 94e73b97b6
44 changed files with 415 additions and 56 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

5
README.md Normal file
View File

@@ -0,0 +1,5 @@
# Recipin API
This is my reworked REST API for my built-from-scratch recipe-sharing platform.
I am rebuilding this API using Spring Boot connected to a PostgreSQL database.

20
pom.xml
View File

@@ -17,28 +17,30 @@
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/main/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/main/java/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/main/java/com/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,9 +1,17 @@
package com.innocuoussymmetry.recipin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableAutoConfiguration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.innocuoussymmetry.recipin"})
@EntityScan(basePackages = {"com.innocuoussymmetry.recipin"})
public class RecipinApplication {
public static void main(String[] args) {

View File

@@ -0,0 +1,35 @@
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.Optional;
@RestController
@RequestMapping("/user")
public class UserController {
private final UserRepository userRepository;
@Autowired
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping
public Iterable<User> getAllUsers() {
return userRepository.findAll();
}
@GetMapping("/{id}")
public User getUserById(Integer id) {
Optional<User> maybe = userRepository.findById(id);
if (maybe.isEmpty()) {
return null;
}
return maybe.get();
}
}

View File

@@ -1,6 +1,7 @@
package com.innocuoussymmetry.recipin.models;
abstract class AppList extends DBEntity {
abstract class AppList {
private Integer id;
private String name;
private Integer ownerId;

View File

@@ -1,14 +0,0 @@
package com.innocuoussymmetry.recipin.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
abstract class DBEntity {
@Id
@Column(name="id")
@GeneratedValue
private Integer id;
}

View File

@@ -1,10 +1,15 @@
package com.innocuoussymmetry.recipin.models;
import javax.persistence.Column;
import javax.persistence.Table;
import jakarta.persistence.*;
@Entity
@Table(name="friendships")
public class Friendship extends DBEntity {
public class Friendship {
@Id
@Column(name="name")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="senderid")
private Integer senderId;
@Column(name="targetid")

View File

@@ -1,28 +1,18 @@
package com.innocuoussymmetry.recipin.models;
import javax.persistence.Column;
import javax.persistence.Table;
import jakarta.persistence.*;
@Entity
@Table(name="grocerylist")
class GroceryList extends AppList {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="name")
private String name;
@Column(name="ownerid")
private Integer ownerId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOwnerId() {
return ownerId;
}
public void setOwnerId(Integer ownerId) {
this.ownerId = ownerId;
}
}

View File

@@ -1,10 +1,15 @@
package com.innocuoussymmetry.recipin.models;
import javax.persistence.Column;
import javax.persistence.Table;
import jakarta.persistence.*;
@Entity
@Table(name="ingredient")
public class Ingredient extends DBEntity {
public class Ingredient {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="name")
private String name;
@Column(name="description")

View File

@@ -1,18 +1,26 @@
package com.innocuoussymmetry.recipin.models;
import javax.persistence.Column;
import javax.persistence.Table;
import jakarta.persistence.*;
import java.util.List;
@Table(name="recipe")
public class Recipe extends DBEntity {
public class Recipe {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="name")
private String name;
@Column(name="preptime")
private String prepTime;
@Column(name="authoruserid")
private Integer authorUserId;
@Column(name="description")
private String description;
@Column(name="ingredientnames")
private List<String> ingredientNames;

View File

@@ -1,10 +1,15 @@
package com.innocuoussymmetry.recipin.models;
import javax.persistence.Column;
import javax.persistence.Table;
import jakarta.persistence.*;
@Entity
@Table(name="recipelist")
public class RecipeList extends AppList {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="ismainlist")
private boolean isMainRecipeList;

View File

@@ -1,19 +1,30 @@
package com.innocuoussymmetry.recipin.models;
import javax.persistence.*;
import jakarta.persistence.*;
@Entity
@Table(name="users")
public class User extends DBEntity {
public class User {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
@Column(name="handle")
private String handle;
@Column(name="email")
private String email;
@Column(name="isadmin")
private boolean isAdmin;
@Column(name="password")
private String password;

Binary file not shown.

View File

@@ -0,0 +1,103 @@
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;
public DBSeed(String constring) {
this.constring = constring;
}
private void connect() {
try {
this.conn = DriverManager.getConnection(constring);
} catch (SQLException e) {
System.out.println(e);
}
}
private void disconnect() {
try {
if (this.conn.isClosed()) {
return;
}
this.conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
private void seed() {
this.connect();
try {
Statement statement = conn.createStatement();
String[] orderedPaths = {
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createappusers.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createingredient.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createcollection.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/creategrocerylist.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createcuisine.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createcourse.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createrecipe.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createrecipecomments.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createcmp_recipeingredient.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createcmp_recipecollection.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createcmp_usersubscriptions.sql",
"src/main/java/com/innocuoussymmetry/recipin/repositories/sql/create/createcmp_userfriendships.sql",
};
File[] pathObjects = new File[orderedPaths.length];
int i = 0;
for (String path : orderedPaths) {
pathObjects[i] = new File(path);
i++;
}
i = 0;
while (i < orderedPaths.length) {
String statementBody = "";
FileReader fr = new FileReader(pathObjects[i]);
BufferedReader br = new BufferedReader(fr);
var lines = br.lines().toArray();
for (var line : lines) {
statementBody += line + " ";
}
statement.execute(statementBody);
i++;
}
statement.close();
} catch (Exception e) {
System.out.println(e);
}
this.disconnect();
}
public static void main(String[] args) {
new DBSeed("jdbc:postgresql://localhost:5432/recipinV2?username=postgres&password=postgres").seed();
}
}

View File

@@ -0,0 +1,22 @@
package com.innocuoussymmetry.recipin.repositories;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.*;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.postgresql.Driver");
dataSourceBuilder.url("jdbc:postgresql://localhost:5432/recipinV2?username=postgres&password=postgres");
dataSourceBuilder.username("postgres");
dataSourceBuilder.password("postgres");
return dataSourceBuilder.build();
}
}

View File

@@ -0,0 +1,7 @@
package com.innocuoussymmetry.recipin.repositories;
import com.innocuoussymmetry.recipin.models.*;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {
}

View File

@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS appusers (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
firstname varchar NOT NULL,
lastname varchar NOT NULL,
handle varchar NOT NULL UNIQUE,
email varchar NOT NULL UNIQUE,
password varchar NOT NULL,
active boolean NOT NULL,
isadmin boolean NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL
);

View File

@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS cmp_recipecollection (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
recipeid int REFERENCES recipe (id),
collectionid int REFERENCES collection (id)
);

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS cmp_recipeingredient (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
quantity decimal NOT NULL,
unit varchar NOT NULL,
ingredientid int REFERENCES ingredient (id),
recipeid int REFERENCES recipe (id)
);

View File

@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS cmp_userfriendships (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
datecreated varchar NOT NULL,
active boolean NOT NULL,
pending boolean NOT NULL,
dateterminated varchar,
senderid int REFERENCES appusers (id),
targetid int REFERENCES appusers (id)
);

View File

@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS cmp_usersubscriptions (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
collectionid int REFERENCES collection (id),
usermemberid int REFERENCES appusers (id),
active boolean NOT NULL
);

View File

@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS collection (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
active boolean NOT NULL,
ismaincollection boolean NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
ownerid int REFERENCES appusers (id)
);

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS course (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
active boolean NOT NULL
);

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS cuisine (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
active boolean NOT NULL
);

View File

@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS grocerylist (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
active boolean NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
ownerid int REFERENCES appusers (id)
);

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS ingredient (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
description varchar,
datecreated varchar NOT NULL,
createdbyid int REFERENCES appusers (id)
);

View File

@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS recipe (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
preptime varchar NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
description varchar,
authoruserid int REFERENCES appusers (id),
cuisineid int REFERENCES cuisine (id),
courseid int REFERENCES course (id)
);

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS recipecomments (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
commentbody varchar NOT NULL,
datecreated varchar NOT NULL,
recipeid int REFERENCES recipe (id) NOT NULL,
authorid int REFERENCES appusers (id) NOT NULL
);

View File

@@ -0,0 +1,3 @@
-- SELECT * FROM cmp_usersubscriptions
-- WHERE usermemberid = $1
-- AND collectionid = $2;

View File

@@ -0,0 +1,13 @@
SELECT
recipin.cmp_userfriendships.id,
recipin.cmp_userfriendships.datecreated,
recipin.appusers.id,
recipin.appusers.firstname,
recipin.appusers.lastname,
recipin.appusers.handle,
recipin.appusers.email
FROM recipin.cmp_userfriendships
INNER JOIN recipin.appusers
ON recipin.appusers.id = recipin.cmp_userfriendships.targetid
WHERE senderid = $1 OR targetid = $1
AND cmp_userfriendships.active = true;

View File

@@ -0,0 +1,5 @@
-- TO DO
-- run a query that selects:
-- 1) all recipes from a user's own collection
-- 2) all recipes from user's subscribed collections
-- 3) all matching a given user id

View File

@@ -0,0 +1,9 @@
SELECT * FROM recipin.recipe
WHERE authoruserid = $1
OR authoruserid IN (
SELECT targetid FROM recipin.cmp_userfriendships
WHERE senderid = $1
UNION
SELECT senderid FROM recipin.cmp_userfriendships
WHERE targetid =$ 1
);

View File

@@ -0,0 +1,2 @@
SELECT * FROM recipin.recipe
WHERE authorid = $1;

View File

@@ -0,0 +1,25 @@
SELECT
recipin.cmp_userfriendships.id,
recipin.cmp_userfriendships.datecreated,
recipin.appusers.id,
recipin.appusers.firstname,
recipin.appusers.lastname,
recipin.appusers.handle,
recipin.appusers.email
FROM recipin.cmp_userfriendships
INNER JOIN recipin.appusers
ON recipin.appusers.id = recipin.cmp_userfriendships.senderid
WHERE recipin.cmp_userfriendships.id = $1
UNION
SELECT
recipin.cmp_userfriendships.id,
recipin.cmp_userfriendships.datecreated,
recipin.appusers.id,
recipin.appusers.firstname,
recipin.appusers.lastname,
recipin.appusers.handle,
recipin.appusers.email
FROM recipin.cmp_userfriendships
INNER JOIN recipin.appusers
ON recipin.appusers.id = recipin.cmp_userfriendships.targetid
WHERE recipin.cmp_userfriendships.id = $1;

View File

@@ -0,0 +1,13 @@
SELECT
recipin.cmp_usersubscriptions.collectionid as collectionid,
recipin.collection.ownerid as ownerid,
recipin.cmp_usersubscriptions.usermemberid as memberid,
recipin.collection.name as collectionname,
recipin.appusers.firstname as owner_first,
recipin.appusers.lastname as owner_last
FROM recipin.collection
INNER JOIN recipin.appusers
ON recipin.collection.ownerid = recipin.appusers.id
INNER JOIN recipin.cmp_usersubscriptions
ON recipin.collection.id = recipin.cmp_usersubscriptions.collectionid
WHERE recipin.cmp_usersubscriptions.usermemberid = $1;

View File

@@ -1 +1,2 @@
spring.datasource.url="jdbc:postgresql://localhost:5432/recipinV2?username=postgres&password=postgres"
spring.datasource.driver-class-name="org.postgresql"