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
55 changes: 55 additions & 0 deletions 351004/Halukha/discussion/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>lab-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>discussion</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.lab.discussion;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LabDiscussionApplication {

public static void main(String[] args) {
SpringApplication.run(LabDiscussionApplication.class, args);
}
}

// netstat -ano | findstr :<PORT>
// taskkill /PID <PID> /F
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.lab.controller;
package com.example.lab.discussion.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -13,9 +12,9 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.lab.dto.PostRequestTo;
import com.example.lab.dto.PostResponseTo;
import com.example.lab.service.PostService;
import com.example.lab.discussion.dto.PostRequestTo;
import com.example.lab.discussion.dto.PostResponseTo;
import com.example.lab.discussion.service.PostService;

import jakarta.validation.Valid;

Expand Down Expand Up @@ -54,4 +53,4 @@ public ResponseEntity<Void> deletePost(@PathVariable Long id) {
postService.deletePost(id);
return ResponseEntity.noContent().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.lab.dto;
package com.example.lab.discussion.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand All @@ -13,6 +13,8 @@ public class PostRequestTo {
@Size(min = 2, max = 2048)
private String content;

public PostRequestTo() {}

public PostRequestTo(
@JsonProperty("newsId") Long newsId,
@JsonProperty("content") String content) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.lab.dto;
package com.example.lab.discussion.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.lab.discussion.exception;

public class EntityNotFoundException extends RuntimeException {
private final int errorCode;

public EntityNotFoundException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
}

public int getErrorCode() { return errorCode; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.lab.exception;
package com.example.lab.discussion.exception;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -50,4 +50,4 @@ public ErrorResponse(int errorCode, String errorMessage) {
public int getErrorCode() { return errorCode; }
public String getErrorMessage() { return errorMessage; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.lab.mapper;
package com.example.lab.discussion.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import com.example.lab.dto.PostRequestTo;
import com.example.lab.dto.PostResponseTo;
import com.example.lab.model.Post;
import com.example.lab.discussion.dto.PostRequestTo;
import com.example.lab.discussion.dto.PostResponseTo;
import com.example.lab.discussion.model.Post;

@Mapper
public interface PostMapper {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package com.example.lab.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
package com.example.lab.discussion.model;

import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
import org.springframework.data.cassandra.core.mapping.Column;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

@Entity
@Table(name = "tbl_post")
@Table("tbl_post")
public class Post {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@PrimaryKey
private Long id;

@Column(name = "newsId")
@Column("newsId")
private Long newsId;

@NotBlank
@Size(min = 2, max = 2048)
@Column(name = "content")
@Column("content")
private String content;

public Post() {
Expand All @@ -39,23 +34,23 @@ public Long getId() {
return id;
}

public Long getNewsId() {
return newsId;
}

public String getContent() {
return content;
}

public void setId(Long id) {
this.id = id;
}

public Long getNewsId() {
return newsId;
}

public void setNewsId(Long newsId) {
this.newsId = newsId;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.lab.discussion.repository;

import org.springframework.data.cassandra.repository.CassandraRepository;
import com.example.lab.discussion.model.Post;

public interface PostRepository extends CassandraRepository<Post, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.example.lab.discussion.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import com.example.lab.discussion.dto.PostRequestTo;
import com.example.lab.discussion.dto.PostResponseTo;
import com.example.lab.discussion.exception.EntityNotFoundException;
import com.example.lab.discussion.mapper.PostMapper;
import com.example.lab.discussion.model.Post;
import com.example.lab.discussion.repository.PostRepository;

@Service
public class PostService {

private final PostRepository postRepository;
private final PostMapper mapper = PostMapper.INSTANCE;

private final WebClient webClient;

public PostService(PostRepository postRepository, WebClient.Builder webClientBuilder) {
this.postRepository = postRepository;
this.webClient = webClientBuilder.baseUrl("http://localhost:24110").build();
}

public List<PostResponseTo> getAllPost() {
return postRepository.findAll().stream()
.map(mapper::toDto)
.collect(Collectors.toList());
}

public PostResponseTo getPostById(Long id) {
return postRepository.findById(id)
.map(mapper::toDto)
.orElseThrow(() -> new EntityNotFoundException("Post not found", 40401));
}

public PostResponseTo createPost(PostRequestTo request) {
try {
Boolean exists = webClient.get()
.uri("/api/v1.0/news/{id}", request.getNewsId())
.retrieve()
.toBodilessEntity()
.map(response -> response.getStatusCode().is2xxSuccessful())
.block();

if (exists == null || !exists) {
throw new EntityNotFoundException("News not found", 40401);
}
} catch (Exception e) {
throw new EntityNotFoundException("News not found or service unavailable", 40401);
}

Post post = new Post();
post.setId(System.currentTimeMillis());
post.setNewsId(request.getNewsId());
post.setContent(request.getContent());

return mapper.toDto(postRepository.save(post));
}

public PostResponseTo updatePost(Long id, PostRequestTo request) {
Post existing = postRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Post not found", 40401));
Post updated = mapper.updateEntity(request, existing);
updated.setId(id);
Post saved = postRepository.save(updated);
return mapper.toDto(saved);
}

public void deletePost(Long id) {
if (!postRepository.existsById(id)) {
throw new EntityNotFoundException("Post not found", 40401);
}
postRepository.deleteById(id);
}
}
14 changes: 14 additions & 0 deletions 351004/Halukha/discussion/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
server:
port: 24130

spring:
application:
name: discussion-service

cassandra:
contact-points: localhost
port: 9042
keyspace-name: distcomp
local-datacenter: datacenter1 # Исправлено здесь
schema-action: create_if_not_exists

Loading