Skip to content

Commit

Permalink
Final edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks027 committed Jan 21, 2020
1 parent 8a9ad6a commit 20de23a
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 12 deletions.
36 changes: 31 additions & 5 deletions src/main/java/com/hotdog/controllers/PostsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,50 @@


import com.hotdog.entities.Posts;
import com.hotdog.entities.Tags;
import com.hotdog.entities.User;
import com.hotdog.repositories.PostsRepo;
import com.hotdog.repositories.TagsRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.function.Consumer;

@Controller
public class PostsController {

private final PostsRepo postsRepo;
private final TagsRepo tagsRepo;

public PostsController(PostsRepo postsRepo) {

public PostsController(PostsRepo postsRepo, TagsRepo tagsRepo) {
this.postsRepo = postsRepo;
this.tagsRepo = tagsRepo;
}




@Value("${upload.path}")
private String uploadPath;

@GetMapping(path = "posts/all")
public String allPosts(Map<String, Object> model){
Iterable<Posts> allPosts = postsRepo.findAll();
Iterable<Tags> allTags = tagsRepo.findAll();


model.put("ps", allPosts);
model.put("postTags", allTags);
return "posts/all";
}

Expand All @@ -46,9 +59,13 @@ public String add(@RequestParam String pstName,
@RequestParam String pstText,
@RequestParam("file") MultipartFile file,
@AuthenticationPrincipal User user,
Map<String, Object> model) throws IOException {
@RequestParam String tagStr
) throws IOException {

Posts p = new Posts();



if(file != null){
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()){
Expand All @@ -67,7 +84,16 @@ public String add(@RequestParam String pstName,
p.setPostText(pstText);
p.setAuthor(user);

postsRepo.save(p);
String[] splitTags = tagStr.split(",");


for (String tempSplitTags : splitTags){
Tags tags = new Tags(tempSplitTags);
p.getTags().add(tags);
tags.getPosts().add(p);
tagsRepo.save(tags);
postsRepo.save(p);
}

return "redirect:/posts/all";
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/hotdog/controllers/TagsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hotdog.controllers;

import com.hotdog.repositories.PostsRepo;
import com.hotdog.repositories.TagsRepo;
import org.springframework.stereotype.Controller;

@Controller
public class TagsController {

private final TagsRepo tagsRepo;
public TagsController(TagsRepo tagsRepo) {
this.tagsRepo = tagsRepo;
}


}
19 changes: 18 additions & 1 deletion src/main/java/com/hotdog/entities/Posts.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "pst")
Expand All @@ -24,6 +29,18 @@ public class Posts {
@JoinColumn(name = "user_id")
private User author;

public Set<Tags> getTags() {
return tags;
}

public void setTags(Set<Tags> tags) {
this.tags = tags;
}

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "posts")
private Set<Tags> tags = new HashSet<>();


public Posts() {}

public User getAuthor() {
Expand All @@ -39,7 +56,7 @@ public void setAuthor(User author) {
}

public LocalDateTime getTimeStamp() {
return timeStamp;
return timeStamp.truncatedTo(ChronoUnit.MINUTES);
}

public void setTimeStamp(LocalDateTime timeStamp) {
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/hotdog/entities/Tags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.hotdog.entities;



import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "tags")
public class Tags {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int tag_id;
@NotNull
private String tag_name;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "post_tags",
joinColumns = {@JoinColumn(name = "tag_id")},
inverseJoinColumns = {@JoinColumn(name = "post_id")}
)
Set<Posts> posts = new HashSet<>();

protected Tags() {}

public Tags(String tag_name){
this.tag_name = tag_name;
}

public int getTag_id() {
return tag_id;
}

public void setTag_id(int tag_id) {
this.tag_id = tag_id;
}

public String getTag_name() {
return tag_name;
}

public void setTag_name(String tag_name) {
this.tag_name = tag_name;
}

public Set<Posts> getPosts() {
return posts;
}

public void setPosts(Set<Posts> posts) {
this.posts = posts;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/hotdog/repositories/TagsRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hotdog.repositories;

import com.hotdog.entities.Posts;
import com.hotdog.entities.Tags;
import org.springframework.data.repository.CrudRepository;

public interface TagsRepo extends CrudRepository<Tags, Long> {

}
5 changes: 5 additions & 0 deletions src/main/resources/templates/greeting.ftlh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<#import "parts/common.ftlh" as comm>
<#import "parts/navbar.ftlh" as navbar>


<@comm.page>
<@navbar.nav/>
<div >
<img src="/img/logo/bg.jpg" style="width: 100% ; opacity: 50%">
</div>
</@comm.page>
27 changes: 22 additions & 5 deletions src/main/resources/templates/parts/postsList.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@
<div class="row">
<div class="col-sm-8 " style="background-color:lavender;">
<#list ps as post>
<div class="card mb-3 img-fluid" style="width:800px">
<div class="card mb-3 img-fluid" style="width:850;margin-top: 10px">
<div class="card-body" style="background-color: #e5ffea">
<h4 class="card-title">${post.postName}</h4>
<div class="row">
<div class="col-sm-9">
<h4 class="card-title">${post.postName}</h4>
</div>
<div class="col-sm-3">
<p class="card-text">Author:
<a href="/user-posts/${post.author.id}">${post.authorName}</a>
</p>
</div>


</div>

<p class="card-text">${post.postText}</p>

<a href="/user-posts/${post.author.id}">${post.authorName}</a>

<img class="card-img-bottom mx-auto d-block" src="/img/${post.imgName}" alt="Card image" style="width:75% ">
<div style="font-size: small">
<time>${post.timeStamp}</time>
</div>
</div>
</div>
</#list>
</div>
<div class="col-sm-4" style="background-color:lavenderblush;">
<p>Nohing for now</p>
<div class="col-sm-4" style="background-color:lavender;">
<div style="position: sticky; top: 0 ; margin-top: 10px ; margin-right: 10px ; margin-right: 10px">
<img class="make-me-sticky sidebar-item" src="/img/logo/ad.png" style="display: block ; margin-left: auto; margin-right: auto; ">
</div>

</div>
</div>
</div>
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/templates/posts.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<form action="/posts" method="post" enctype="multipart/form-data">

<div class="form-group">
<label for="pstName">Password:</label>
<label for="pstName">Name:</label>
<input type="pstName" class="form-control" id="pstName" placeholder="Enter name" name="pstName">
</div>

Expand All @@ -20,11 +20,18 @@
<textarea class="form-control" id="pstText" rows="3" name="pstText" placeholder="Post description"></textarea>
</div>

<div class="form-group">
<label for="tagStr">Tag</label>
<textarea class="form-control" id="tagStr" rows="3" name="tagStr" placeholder="Enter tag"></textarea>
</div>

<div class="custom-file">
<input type="file" class="custom-file-input" id="file", name="file">
<label class="custom-file-label" for="file"></label>
</div>



<input type="hidden" name="_csrf" value="${_csrf.token}"/>

<input type="submit" class="btn btn-dark mt-3 mx-auto d-block" value="Create Post"/>
Expand Down

0 comments on commit 20de23a

Please sign in to comment.