Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ services:
db:
image: mariadb
environment:

MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: twitter
MYSQL_USER: dev
Expand All @@ -21,16 +20,16 @@ services:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: root

web:
depends_on:
- db
image: tomcat
volumes:
- ./target/twitter-0.0.1-SNAPSHOT.war:/usr/local/tomcat/webapps/twitter.war
ports:
- '8080:8080'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_application
MYSQL_USER: dev
MYSQL_PASSWORD: dev
#web:
# depends_on:
# - db
# image: tomcat
# volumes:
# - ./target/twitter-0.0.1-SNAPSHOT.war:/usr/local/tomcat/webapps/twitter.war
# ports:
# - '8080:8080'
# environment:
# MYSQL_ROOT_PASSWORD: root
# MYSQL_DATABASE: test_application
# MYSQL_USER: dev
# MYSQL_PASSWORD: dev
22 changes: 12 additions & 10 deletions src/main/java/de/school/twitter/controller/CommentController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package de.school.twitter.controller;

import java.util.List;

import de.school.twitter.model.Comment;
import de.school.twitter.model.Tweet;
import de.school.twitter.repository.CommentRepository;
import de.school.twitter.repository.TweetRepository;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CommentController {
Expand All @@ -28,7 +23,7 @@ public List<Comment> getAllComments(){
return commentRepository.findAll();
}

@PostMapping("tweet/{id}/comment")
@PostMapping("/tweet/{id}/comment")
public void addComment(
@PathVariable("id") Integer aId,
@RequestBody Comment aComment){
Expand All @@ -40,7 +35,14 @@ public void addComment(
}

@GetMapping("/tweet/{id}/comments")
public List<Comment> getAllCommentsForTweet(@PathVariable("id") Integer aId){
public List<Comment> getAllCommentsForTweet(@PathVariable("id") Integer aId) {
return commentRepository.getAllCommentsByTweet(aId);
}

@GetMapping("/tweet/{id}/comment/{comment_id}/like")
public void likeComment(@PathVariable("id") Integer aId, @PathVariable("comment_id") Integer aCommentId) {
Comment comment = commentRepository.getReferenceById(aCommentId);
comment.setLikes(comment.getLikes() + 1);
commentRepository.save(comment);
}
}
25 changes: 14 additions & 11 deletions src/main/java/de/school/twitter/controller/TweetController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package de.school.twitter.controller;

import java.util.List;

import de.school.twitter.model.Tweet;
import de.school.twitter.repository.TweetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TweetController {
Expand All @@ -32,13 +27,21 @@ public void saveTweet(@RequestBody Tweet aTweet){
}

@GetMapping("/tweet/{id}")
public Tweet getTweetById(@PathVariable("id") Integer aId){
return tweetRepository.getReferenceById(aId);
public Tweet getTweetById(@PathVariable("id") Integer aId) {
return tweetRepository.getReferenceById(aId);
}

//TODO: not working: Cannot delete or update a parent row: a foreign key constraint fails
@DeleteMapping("/tweet/{id}")
public void deleteTweetById(@PathVariable("id") Integer aId){
public void deleteTweetById(@PathVariable("id") Integer aId) {
tweetRepository.deleteById(aId);
}

@GetMapping("/tweet/{id}/like")
public void liketweet(@PathVariable("id") Integer aId) {
Tweet tweet = tweetRepository.getReferenceById(aId);
tweet.setLikes(tweet.getLikes() + 1);
tweetRepository.save(tweet);
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package de.school.twitter.repository;

import java.util.List;

import de.school.twitter.model.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Integer> {

@Query(value = "SELECT * FROM comments WHERE tweet_id_id = :id", nativeQuery = true)
List<Comment> getAllCommentsByTweet(@Param("id") int aId);

@Override
Comment getReferenceById(Integer integer);


}
19 changes: 10 additions & 9 deletions twitter/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { HttpService, Tweet, Comment } from './http.service';
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Comment, HttpService, Tweet} from './http.service';

@Component({
selector: 'app-root',
Expand All @@ -9,6 +9,7 @@ import { HttpService, Tweet, Comment } from './http.service';
export class AppComponent implements OnInit, AfterViewInit {
tweets!: Tweet[]
port: string = '8080'
url: string = 'https://twatter.vetrix.dev'
height: number = window.innerHeight
@ViewChild('content') contentDiv!: ElementRef<HTMLDivElement>;

Expand All @@ -24,7 +25,7 @@ export class AppComponent implements OnInit, AfterViewInit {
}

async setTweets(){
this.tweets = (<Tweet[]>await this.http.get(`http://127.0.0.1:${this.port}/tweet`))
this.tweets = (<Tweet[]>await this.http.get(`${this.url}/twitter/`))

for(var i of this.tweets){
i.showComments = false
Expand All @@ -33,7 +34,7 @@ export class AppComponent implements OnInit, AfterViewInit {

async setComments(item: any, overWrite: boolean = false): Promise<boolean>{
if(item.comments == undefined || overWrite == true){
item.comments = <Comment[]>await this.http.get(`http://127.0.0.1:${this.port}/tweet/${item.id}/comment`)
item.comments = <Comment[]>await this.http.get(`${this.url}/twitter/tweet/${item.id}/comments`)
item.showComments = true
return true
}
Expand Down Expand Up @@ -89,7 +90,7 @@ export class AppComponent implements OnInit, AfterViewInit {
}

async submitComment(comment: string, id: number){
await this.http.post(`http://127.0.0.1:${this.port}/tweet/${id}/comment`, { content: comment })
await this.http.post(`${this.url}/twitter/tweet/${id}/comment`, {content: comment})

for(var i of this.tweets){
if(i.id == id){
Expand All @@ -103,24 +104,24 @@ export class AppComponent implements OnInit, AfterViewInit {
async submitTweet(tweet: string){
this.height = this.getHeight()

await this.http.post(`http://127.0.0.1:${this.port}/tweet`, { content: tweet })
await this.http.post(`${this.url}/twitter/tweet`, {content: tweet})
await this.setTweets()
}

async likeTweet(id: number){
await this.http.get(`http://127.0.0.1:${this.port}/tweet/${id}/like`)
await this.http.get(`${this.url}/twitter/tweet/${id}/like`)

this.tweets[ this.tweetIndexById(id) ].likes++
}

async likeComment(tid: number, cid: number){
await this.http.get(`http://127.0.0.1:${this.port}/tweet/${tid}/comment/${cid}/like`)
await this.http.get(`${this.url}/twitter/tweet/${tid}/comment/${cid}/like`)

this.tweets[ this.tweetIndexById(tid) ].comments[ this.commentIndexById(tid, cid) ].likes++
}

async deleteTweet(id: number){
await this.http.delete(`http://127.0.0.1:${this.port}/tweet/${id}`)
await this.http.delete(`${this.url}/twitter/tweet/${id}`)
await this.setTweets()
}
}