Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

31 refactoring old api endpoints #32

Merged
merged 3 commits into from
Jul 8, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.student.api;

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.student.api.models.ArtifactCreateRequest;
import org.student.api.models.ArtifactLoadResponse;
import org.student.api.models.ArtifactResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.UUID;

public interface ArtifactsController {
@PostMapping("/upload")
Mono<ArtifactResponse> uploadArtifact(@RequestBody ArtifactCreateRequest request);

@GetMapping("/getAll")
Flux<ArtifactResponse> getAllArtifacts();

@GetMapping("/loadArtifact/{id}")
Mono<ArtifactLoadResponse> loadArtifact(@PathVariable UUID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.student.api;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.student.api.models.ArtifactCreateRequest;
import org.student.api.models.ArtifactLoadResponse;
import org.student.api.models.ArtifactResponse;
import org.student.services.ArtifactsService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("api/v1/artifacts")
public class ArtifactsControllerImpl implements ArtifactsController {

private static final Logger logger = LogManager.getLogger(ArtifactsControllerImpl.class);

private final ArtifactsService artifactsService;

public ArtifactsControllerImpl(ArtifactsService artifactsService) {
this.artifactsService = artifactsService;
}

@Override
@PostMapping("/upload")
public Mono<ArtifactResponse> uploadArtifact(@RequestBody ArtifactCreateRequest request) {

logger.info("Creating artifact from request: {}", request);

return artifactsService.upload(request).doOnNext(artifactResponse -> logger.info("Created artifact: {}", artifactResponse));
}

@Override
@GetMapping("/getAll")
public Flux<ArtifactResponse> getAllArtifacts() {
return artifactsService.getAllArtifacts()
.doOnNext(artifact -> logger.info("Received artifact info from storage: {}", artifact));
}

@Override
@GetMapping("/loadArtifact/{id}")
public Mono<ArtifactLoadResponse> loadArtifact(@PathVariable UUID id) {
return artifactsService.getArtifactById(id)
.doOnNext(artifact -> logger.info("Loading artifact with id {}", id));
}

}
26 changes: 0 additions & 26 deletions core-back-end/src/main/java/org/student/api/FilesController.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.student.api.models;

import java.util.Arrays;
import java.util.Objects;

public class ArtifactCreateRequest {

private String name;

private byte[] artifactBody;

public ArtifactCreateRequest(String name, byte[] artifactBody) {
this.name = name;
this.artifactBody = artifactBody;
}

public ArtifactCreateRequest() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public byte[] getArtifactBody() {
return artifactBody;
}

public void setArtifactBody(byte[] artifactBody) {
this.artifactBody = artifactBody;
}

@Override
public String toString() {
return "ArtifactCreateRequest{" +
"name='" + name + '\'' +
", artifactBody=" + Arrays.toString(artifactBody) +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArtifactCreateRequest that = (ArtifactCreateRequest) o;
return Objects.equals(name, that.name) && Arrays.equals(artifactBody, that.artifactBody);
}

@Override
public int hashCode() {
int result = Objects.hash(name);
result = 31 * result + Arrays.hashCode(artifactBody);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.student.api.models;

import java.util.Arrays;
import java.util.UUID;

public class ArtifactLoadResponse extends ArtifactResponse{

private byte[] artifactBody;

public ArtifactLoadResponse(byte[] artifactBody) {
this.artifactBody = artifactBody;
}

public ArtifactLoadResponse(UUID id, ArtifactMateInfo metaInfo, byte[] artifactBody) {
super(id, metaInfo);
this.artifactBody = artifactBody;
}


public byte[] getArtifactBody() {
return artifactBody;
}

public void setArtifactBody(byte[] artifactBody) {
this.artifactBody = artifactBody;
}

@Override
public String toString() {
return "ArtifactLoadResponse{" +
"artifactBody=" + Arrays.toString(artifactBody) +
"} " + super.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArtifactLoadResponse that = (ArtifactLoadResponse) o;
return Arrays.equals(artifactBody, that.artifactBody);
}

@Override
public int hashCode() {
return Arrays.hashCode(artifactBody);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.student.api.models;

import java.util.Objects;

public class ArtifactMateInfo {
private String artifactName;
private long artifactSize;

public ArtifactMateInfo(String artifactName, long artifactSize) {
this.artifactName = artifactName;
this.artifactSize = artifactSize;
}

public ArtifactMateInfo() {
}

public String getArtifactName() {
return artifactName;
}

public void setArtifactName(String artifactName) {
this.artifactName = artifactName;
}

public long getArtifactSize() {
return artifactSize;
}

public void setArtifactSize(long artifactSize) {
this.artifactSize = artifactSize;
}

@Override
public String toString() {
return "ArtifactMateInfo{" +
"artifactName='" + artifactName + '\'' +
", artifactSize=" + artifactSize +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArtifactMateInfo that = (ArtifactMateInfo) o;
return artifactSize == that.artifactSize && Objects.equals(artifactName, that.artifactName);
}

@Override
public int hashCode() {
return Objects.hash(artifactName, artifactSize);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.student.api.models;

import java.util.Objects;
import java.util.UUID;

public class ArtifactResponse {
private UUID id;

private ArtifactMateInfo metaInfo;

public ArtifactResponse(UUID id, ArtifactMateInfo metaInfo) {
this.id = id;
this.metaInfo = metaInfo;
}

public ArtifactResponse() {
}

public UUID getId() {
return id;
}

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

public ArtifactMateInfo getMetaInfo() {
return metaInfo;
}

public void setMetaInfo(ArtifactMateInfo metaInfo) {
this.metaInfo = metaInfo;
}

@Override
public String toString() {
return "ArtifactCreateResponce{" +
"id=" + id +
", metaInfo=" + metaInfo +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArtifactResponse response = (ArtifactResponse) o;
return Objects.equals(id, response.id) && Objects.equals(metaInfo, response.metaInfo);
}

@Override
public int hashCode() {
return Objects.hash(id, metaInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.student.services;

import org.springframework.stereotype.Service;
import org.student.api.models.ArtifactCreateRequest;
import org.student.api.models.ArtifactLoadResponse;
import org.student.api.models.ArtifactResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.UUID;

@Service
public interface ArtifactsService {


Mono<ArtifactResponse> upload(ArtifactCreateRequest request);

Flux<ArtifactResponse> getAllArtifacts();

Mono<ArtifactLoadResponse> getArtifactById(UUID id);
}
Loading
Loading