few improvements

This commit is contained in:
2025-03-27 15:02:54 +01:00
parent ea52a0060a
commit fdee6ecc84
7 changed files with 196 additions and 14 deletions

45
pom.xml
View File

@@ -38,10 +38,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId> <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>org.springframework.boot</groupId> <!-- <groupId>org.springframework.boot</groupId>-->
<artifactId>spring-boot-starter-oauth2-client</artifactId> <!-- <artifactId>spring-boot-starter-oauth2-client</artifactId>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
@@ -53,12 +53,12 @@
<scope>runtime</scope> <scope>runtime</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>org.springframework.boot</groupId> <!-- <groupId>org.springframework.boot</groupId>-->
<artifactId>spring-boot-docker-compose</artifactId> <!-- <artifactId>spring-boot-docker-compose</artifactId>-->
<scope>runtime</scope> <!-- <scope>runtime</scope>-->
<optional>true</optional> <!-- <optional>true</optional>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
@@ -70,9 +70,18 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-security-test</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
<scope>test</scope> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency> </dependency>
</dependencies> </dependencies>
@@ -82,6 +91,16 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/mockito/mockito-core/${mockito.version}/mockito-core-${mockito.version}.jar
-Xshare:off
</argLine>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>

View File

@@ -0,0 +1,28 @@
package _11.asktpk.artisanconnectbackend.Controller;
import _11.asktpk.artisanconnectbackend.Model.Notice;
import _11.asktpk.artisanconnectbackend.Repository.NoticeRepository;
import _11.asktpk.artisanconnectbackend.Service.PostgresDatabase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping("/api/v1")
@RestController
public class ArtisanConnectController {
@Autowired
private PostgresDatabase postgresDatabase;
@GetMapping("/notices/all")
public List<Notice> getAllNotices() {
return postgresDatabase.get();
}
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/notices/add")
public void addNotice(@RequestBody Notice notice) {
postgresDatabase.add(notice);
}
}

View File

@@ -0,0 +1,63 @@
package _11.asktpk.artisanconnectbackend.Model;
import jakarta.persistence.*;
import java.sql.Blob;
@Entity
public class Notice {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String title;
@Column
private String username;
@Column
private String description;
public Notice() {
this.title = "";
this.username = "";
this.description = "";
}
public Notice(String nTitle, String nUsername, String nDescription) {
this.title = nTitle;
this.username = nUsername;
this.description = nDescription;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public void setUsername(String user) {
this.username = user;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@@ -0,0 +1,8 @@
package _11.asktpk.artisanconnectbackend.Repository;
import _11.asktpk.artisanconnectbackend.Model.Notice;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NoticeRepository extends JpaRepository<Notice, Long> {
}

View File

@@ -0,0 +1,10 @@
package _11.asktpk.artisanconnectbackend.Service;
import _11.asktpk.artisanconnectbackend.Model.Notice;
import java.util.List;
public interface IDatabase {
void add(Notice newNotice);
List<Notice> get();
}

View File

@@ -0,0 +1,24 @@
package _11.asktpk.artisanconnectbackend.Service;
import _11.asktpk.artisanconnectbackend.Model.Notice;
import _11.asktpk.artisanconnectbackend.Repository.NoticeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PostgresDatabase implements IDatabase{
@Autowired
private NoticeRepository noticeRepository;
@Override
public void add(Notice newNotice) {
noticeRepository.save(newNotice);
}
@Override
public List<Notice> get() {
return noticeRepository.findAll();
}
}

View File

@@ -1,13 +1,43 @@
package _11.asktpk.artisanconnectbackend; package _11.asktpk.artisanconnectbackend;
import _11.asktpk.artisanconnectbackend.Model.Notice;
import _11.asktpk.artisanconnectbackend.Service.PostgresDatabase;
import okhttp3.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest @SpringBootTest
class ArtisanConnectBackendApplicationTests { class ArtisanConnectBackendApplicationTests {
private static final Logger logger = LogManager.getLogger(ArtisanConnectBackendApplicationTests.class);
@Autowired
PostgresDatabase postgresDatabase;
@Test @Test
void contextLoads() { void testPostgresDatabase() {
postgresDatabase.add(new Notice("Test Notice", "Username", "Test Description"));
Boolean isRecordAvailable = postgresDatabase.get().size() > 0;
if(isRecordAvailable) {
logger.info("The record is available in the database");
} else {
logger.error("The record is not available in the database");
}
assert isRecordAvailable;
} }
@Test
void getAllNotices() throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
Request request = new Request.Builder()
.url("http://localhost:8080/api/v1/notices/all")
.build();
Response response = client.newCall(request).execute();
}
} }