- Добавил поле автор в сообщение.
https://www.youtube.com/watch?v=PpoOoR55Ypw&list=PLU2ftbIeotGpAYRP9Iv2KLIwK36-o_qYk&index=6
This commit is contained in:
@@ -1,7 +1,12 @@
|
|||||||
package net.sytes.kashey.sweater.controller;
|
package net.sytes.kashey.sweater.controller;
|
||||||
|
|
||||||
import net.sytes.kashey.sweater.domain.Message;
|
import net.sytes.kashey.sweater.domain.Message;
|
||||||
|
import net.sytes.kashey.sweater.domain.User;
|
||||||
import net.sytes.kashey.sweater.repository.MessageRepository;
|
import net.sytes.kashey.sweater.repository.MessageRepository;
|
||||||
|
import net.sytes.kashey.sweater.repository.UserRepository;
|
||||||
|
import net.sytes.kashey.sweater.security.CustomUserDetails;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@@ -13,9 +18,11 @@ import java.util.Map;
|
|||||||
public class MainController {
|
public class MainController {
|
||||||
|
|
||||||
private final MessageRepository messageRepository;
|
private final MessageRepository messageRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public MainController(MessageRepository messageRepository) {
|
public MainController(MessageRepository messageRepository, UserRepository userRepository) {
|
||||||
this.messageRepository = messageRepository;
|
this.messageRepository = messageRepository;
|
||||||
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
@@ -31,9 +38,15 @@ public class MainController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/main")
|
@PostMapping("/main")
|
||||||
public String addMessage(@RequestParam String text, @RequestParam String tag, Map<String, Object> model) {
|
public String addMessage(@AuthenticationPrincipal CustomUserDetails details,
|
||||||
|
@RequestParam String text,
|
||||||
|
@RequestParam String tag,
|
||||||
|
Map<String, Object> model) {
|
||||||
|
|
||||||
messageRepository.save(new Message(text, tag));
|
User author = userRepository.findByUsername(details.getUsername())
|
||||||
|
.orElseThrow(() -> new UsernameNotFoundException("User not found: " + details.getUsername()));
|
||||||
|
|
||||||
|
messageRepository.save(new Message(text, tag, author));
|
||||||
model.put("messages", messageRepository.findAll());
|
model.put("messages", messageRepository.findAll());
|
||||||
return "main";
|
return "main";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package net.sytes.kashey.sweater.domain;
|
package net.sytes.kashey.sweater.domain;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.GeneratedValue;
|
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Message {
|
public class Message {
|
||||||
@@ -13,6 +10,10 @@ public class Message {
|
|||||||
private String text;
|
private String text;
|
||||||
private String tag;
|
private String tag;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
private User author;
|
||||||
|
|
||||||
public Message() {
|
public Message() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,6 +22,12 @@ public class Message {
|
|||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Message(String text, String tag, User author) {
|
||||||
|
this.text = text;
|
||||||
|
this.tag = tag;
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@@ -44,4 +51,16 @@ public class Message {
|
|||||||
public void setTag(String tag) {
|
public void setTag(String tag) {
|
||||||
this.tag = tag;
|
this.tag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(User author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthorName() {
|
||||||
|
return author == null ? "<none>": author.getUsername();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
<b>{{id}}</b>
|
<b>{{id}}</b>
|
||||||
<span>{{text}}</span>
|
<span>{{text}}</span>
|
||||||
<i>{{tag}}</i>
|
<i>{{tag}}</i>
|
||||||
|
<strong>{{authorName}}</strong>
|
||||||
</div>
|
</div>
|
||||||
{{/messages}}
|
{{/messages}}
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user