Skip to content

Commit

Permalink
feat/TopsCreator saves tops at the database
Browse files Browse the repository at this point in the history
The tops created through the top creator are saved in the database
  • Loading branch information
Kr4ll committed Mar 1, 2023
1 parent 09d164b commit f85bd86
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 25 deletions.
7 changes: 6 additions & 1 deletion back/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.14.Final</version>
</dependency>

</dependencies>
</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,89 @@
package net.daw.alist.controllers;
import java.io.IOException;
import java.net.URI;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.hibernate.engine.jdbc.BlobProxy;

import net.daw.alist.models.PostItem;
import net.daw.alist.models.Post;
import net.daw.alist.models.Topic;
import net.daw.alist.repositories.TopicRepository;
import net.daw.alist.repositories.PostItemRepository;
import net.daw.alist.repositories.PostRepository;

@Controller
public class CreateListController {

@Autowired
PostItemRepository item;
@Autowired
PostRepository post;
@Autowired
TopicRepository topic;

@GetMapping("/create-list")
public String createList() {

return "create-list";
}

}
@PostMapping("/create")
public String saveData(
@RequestParam String topTitle, @RequestParam String topicList,
@RequestParam String formDescriptionOne, @RequestParam MultipartFile formFileOne,
@RequestParam String formDescriptionTwo, @RequestParam MultipartFile formFileTwo,
@RequestParam (required=false) String formDescriptionThree, @RequestParam (required=false)MultipartFile formFileThree,
@RequestParam (required=false)String formDescriptionFour, @RequestParam (required=false)MultipartFile formFileFour,
@RequestParam (required=false)String formDescriptionFive, @RequestParam (required=false)MultipartFile formFileFive
) throws IOException{
List<PostItem> postList=new ArrayList<>();

URI location = ServletUriComponentsBuilder.fromCurrentRequest().build().toUri();

/*Optimize it with a loop when posbile */
PostItem item1 = new PostItem((String)formDescriptionOne, (BlobProxy.generateProxy(formFileOne.getInputStream(), formFileOne.getSize())),
(String) location.toString()+"1");
postList.add(item1);

PostItem item2 = new PostItem((String)formDescriptionTwo, (BlobProxy.generateProxy(formFileTwo.getInputStream(), formFileTwo.getSize())),
(String) location.toString()+"2");
postList.add(item2);

if(formDescriptionThree.equals(null)){
PostItem item3 = new PostItem((String)formDescriptionThree, (BlobProxy.generateProxy(formFileThree.getInputStream(), formFileThree.getSize())),
(String) location.toString()+"3");
postList.add(item3);
}

if(formDescriptionFour.equals(null)){
PostItem item4 = new PostItem((String)formDescriptionFour, (BlobProxy.generateProxy(formFileFour.getInputStream(), formFileFour.getSize())),
(String) location.toString()+"4");
postList.add(item4);
}

if(formDescriptionFive.equals(null) ){
PostItem item5 = new PostItem((String)formDescriptionFive, (BlobProxy.generateProxy(formFileFive.getInputStream(), formFileFive.getSize())),
(String) location.toString()+"5");
postList.add(item5);
}

Topic top = new Topic();
top.setName(topicList);
post.save(new Post(topTitle,postList,top));

return "home";
}


}
2 changes: 1 addition & 1 deletion back/src/main/java/net/daw/alist/models/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Comment {
private Date date;
private String content;

@ManyToOne
@ManyToOne (optional=true)
private User author;

@Lob
Expand Down
23 changes: 14 additions & 9 deletions back/src/main/java/net/daw/alist/models/Post.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.daw.alist.models;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.*;
Expand All @@ -22,12 +22,13 @@ public class Post {
private Set<User> upVotes = new HashSet<>();

@OneToMany
private Set<User> downVotes = new HashSet<>();
private Set<User> downVotes = new HashSet<>();

@ManyToMany
@ManyToMany (mappedBy="posts")
private List<Topic> topics = new ArrayList<>();


@OneToMany(cascade = CascadeType.ALL)
@ManyToMany(cascade = CascadeType.ALL)
private List<PostItem> items = new ArrayList<>();

@OneToMany(cascade = CascadeType.ALL)
Expand All @@ -37,13 +38,14 @@ public Post() { }

public Post(
String title,
List<Topic> topics,
List<PostItem> items
List<PostItem> items,
Topic top
) {
topics.add(top);
this.date = new Date();
this.title = title;
this.topics = topics;
this.items = items;

}

public void setTitle(String title) {
Expand All @@ -57,12 +59,13 @@ public void setUpVotes(Set<User> upVotes) {
public void setDownVotes(Set<User> downVotes) {
this.downVotes = downVotes;
}


public void setTopics(List<Topic> topics) {
public void setTopics(ArrayList<Topic> topics) {
this.topics = topics;
}

public void setItems(List<PostItem> items) {
public void setItems(ArrayList<PostItem> items) {
this.items = items;
}

Expand All @@ -78,13 +81,15 @@ public String getTitle() {
return title;
}


public Set<User> getUpVotes() {
return upVotes;
}

public Set<User> getDownVotes() {
return downVotes;
}


public List<Topic> getTopics() {
return topics;
Expand Down
2 changes: 1 addition & 1 deletion back/src/main/java/net/daw/alist/models/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Topic {
private String name;
private String description;

@ManyToMany(mappedBy="topics")
@ManyToMany
private List<Post> posts = new ArrayList<>();

public Topic() { }
Expand Down
28 changes: 16 additions & 12 deletions back/src/main/resources/templates/create-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

<body class="text-center">
{{>navbar}}

<form action="/create" method="post" enctype="multipart/form-data" >
<div class="accordion mt-3 mb-3" id="accordion">

<input class="form-control mb-2" id="topTitle" placeholder="Write a title...">
<input class="form-control mb-2" list="topicOptions" id="topicList" placeholder="Type to add topic...">
<input class="form-control mb-2" id="topTitle" name="topTitle" placeholder="Write a title...">
<input class="form-control mb-2" list="topicOptions" id="topicList" name="topicList" placeholder="Type to add topic...">
<datalist id="topicOptions">
<option value="Sports"> </option>
<option value="Anime"> </option>
Expand All @@ -43,9 +45,9 @@ <h2 class="accordion-header" id="headingOne">
data-bs-parent="#accordion">
<div class="accordion-body">
<img class="img-fluid rounded mt-3" id="imgOne" src="#" alt="">
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionOne"
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionOne" name="formDescriptionOne"
placeholder="Write a description...">
<input class="form-control" type="file" accept="image/*" id="formFileOne"
<input class="form-control" type="file" accept="image/*" id="formFileOne" name="formFileOne"
onchange="loadFile(event, 'imgOne')">
</div>
</div>
Expand All @@ -62,9 +64,9 @@ <h2 class="accordion-header" id="headingTwo">
data-bs-parent="#accordion">
<div class="accordion-body">
<img class="img-fluid rounded mt-3" id="imgTwo" src="#" alt="">
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionTwo"
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionTwo" name="formDescriptionTwo"
placeholder="Write a description...">
<input class="form-control" type="file" accept="image/*" id="formFileTwo"
<input class="form-control" type="file" accept="image/*" id="formFileTwo" name="formFileTwo"
onchange="loadFile(event, 'imgTwo')">
</div>
</div>
Expand All @@ -81,9 +83,9 @@ <h2 class="accordion-header" id="headingThree">
data-bs-parent="#accordion">
<div class="accordion-body">
<img class="img-fluid rounded mt-3" id="imgThree" src="#" alt="">
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionThree"
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionThree" name="formDescriptionThree"
placeholder="Write a description...">
<input class="form-control" type="file" accept="image/*" id="formFileThree"
<input class="form-control" type="file" accept="image/*" id="formFileThree" name="formFileThree"
onchange="loadFile(event, 'imgThree')">
</div>
</div>
Expand All @@ -100,9 +102,9 @@ <h2 class="accordion-header" id="headingFour">
data-bs-parent="#accordion">
<div class="accordion-body">
<img class="img-fluid rounded mt-3" id="imgFour" src="#" alt="">
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionFour"
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionFour" name="formDescriptionFour"
placeholder="Write a description...">
<input class="form-control" type="file" accept="image/*" id="formFileFour"
<input class="form-control" type="file" accept="image/*" id="formFileFour" name="formFileFour"
onchange="loadFile(event, 'imgFour')">
</div>
</div>
Expand All @@ -119,17 +121,19 @@ <h2 class="accordion-header" id="headingFive">
data-bs-parent="#accordion">
<div class="accordion-body">
<img class="img-fluid rounded mt-3" id="imgFive" src="#" alt="">
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionFive"
<input class="form-control mt-3 mb-2" type="text" id="formDescriptionFive" name="formDescriptionFive"
placeholder="Write a description...">
<input class="form-control" type="file" accept="image/*" id="formFileFive"
<input class="form-control" type="file" accept="image/*" id="formFileFive" name="formFileFive"
onchange="loadFile(event, 'imgFive')">
</div>
</div>
</div>

<button class="w-100 btn btn-lg btn-primary mt-2" type="submit">Create</button>


</div>
</form>

</body>

Expand Down

0 comments on commit f85bd86

Please sign in to comment.