Skip to content

Commit

Permalink
Final code cleanup, unti test, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul V R committed Oct 26, 2023
1 parent 4cf288b commit 4e26f31
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 195 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ dependencies {
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'

runtimeOnly 'com.h2database:h2'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

testImplementation 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/project/open_hands/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public final class Constants {
private Constants() {
}

public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss[.SSSSSS]";
public static final String UTC = "UTC";
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSSSSS]");
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
import com.project.open_hands.entity.Image;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;


public interface ImageRepository extends JpaRepository<Image, Long> {
List<Image> findByPostId(String postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import com.project.open_hands.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Collection;

public interface PostRepository extends JpaRepository<Post, Long> {

Collection<Post> createdBy(String email);
Post title(String title);
Post findByTitle(String title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import java.util.List;
import java.util.Optional;

@RestController
//@CrossOrigin(origins = "http://localhost:8082") open for specific port
@RequiredArgsConstructor
@CrossOrigin
@RestController
@RequestMapping(path = "/images")
@RequiredArgsConstructor
public class ImageController {

private final ImageService imageService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import java.util.List;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/messages")
@RequiredArgsConstructor
@Slf4j
public class MessageController {
private final MessageRepository messageRepo;
private final PostService postService;
Expand All @@ -37,6 +37,29 @@ public ResponseEntity<Message> create(@RequestBody @Valid MessageRequest message
return ResponseEntity.status(HttpStatus.CREATED).body(msg);
}

@GetMapping("/from/{email}")
public ResponseEntity<List<Message>> getMessagesFrom(@Valid @PathVariable("email") String email) {
// Request that I made to other (my email will be in from field)
log.info("Requesting from, {}", email);
List<Message> messages = messageRepo.findByFromEmail(email);
if (messages.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NO_CONTENT, "No data from" + " - " + email);
}

return ResponseEntity.ok(messages);
}

@GetMapping("/to/{email}")
public ResponseEntity<List<Message>> getMessages(@Valid @PathVariable("email") String email) {
log.info("Requesting to, {}", email);
List<Message> messages = messageRepo.findByToEmail(email);
if (messages.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NO_CONTENT, "No data to " + email);
}

return ResponseEntity.ok(messages);
}

private static Message toMessage(MessageRequest messageRequest, Post post) {
Message entity = new Message();
entity.setName(messageRequest.getName());
Expand All @@ -48,23 +71,4 @@ private static Message toMessage(MessageRequest messageRequest, Post post) {
entity.setPost(post);
return entity;
}

@GetMapping("/{fromOrTo}/{email}")
public ResponseEntity<List<Message>> getMessages(@Valid @PathVariable("fromOrTo") String fromOrTo, @Valid @PathVariable("email") String email) {
// Request that I made to other (my email will be in from field)
log.info("Requesting {}, {}", fromOrTo, email);
List<Message> messages;
if ("from".equals(fromOrTo)) {
messages = messageRepo.findByFromEmail(email);

} else {
messages = messageRepo.findByToEmail(email);
}

if (messages.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NO_CONTENT, "No data " + fromOrTo + " - " + email);
}

return ResponseEntity.ok(messages);
}
}
42 changes: 7 additions & 35 deletions src/main/java/com/project/open_hands/resource/PostController.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,30 @@
package com.project.open_hands.resource;

import com.project.open_hands.entity.Post;
import com.project.open_hands.repository.ImageRepository;
import com.project.open_hands.repository.PostRepository;
import com.project.open_hands.repository.UserRepository;
import com.project.open_hands.services.PostService;
import jakarta.annotation.PostConstruct;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/posts")
@RequiredArgsConstructor
@RequestMapping("/posts")
public class PostController {

private final PostService postService;
private final ImageRepository imageRepository;

@PostMapping(consumes = "application/json;charset=UTF-8")
public ResponseEntity<Post> create(@Valid @RequestBody Post post) {
return ResponseEntity.status(HttpStatus.CREATED).body(postService.create(post));
}

// @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
// public ResponseEntity<Post> createMultipart(@Valid @RequestPart Post post, @RequestPart MultipartFile[] file) {
// Post post1 = postService.create(post);
//
//
// return ResponseEntity.status(HttpStatus.CREATED).body(post1);
// }

@GetMapping(consumes = "application/json;charset=UTF-8")
public ResponseEntity<Collection<Post>> getAllPosts() {
Collection<Post> allPosts = postService.getAllPosts();
Expand Down Expand Up @@ -86,20 +68,10 @@ public ResponseEntity<Void> delete(@PathVariable Long postId) {
return ResponseEntity.accepted().build();
}


// @PostMapping(consumes = "application/json;charset=UTF-8")
// public ResponseEntity<Post> create(@Valid @RequestBody Post postRequest) {
//
// RequestMapper instance = RequestMapper.INSTANCE;
// Post post = instance.toEntity(postRequest);
//
// Optional<User> createdByUser = userRepo.findById(postRequest.getCreatedByEmail());
// if (createdByUser.isEmpty()) {
// throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid createdBy email. User not found!");
// }
// post.setCreatedBy(createdByUser.get());
//
//
// return ResponseEntity.status(HttpStatus.CREATED).body(postRepo.save(post));
// }
@DeleteMapping(consumes = "application/json;charset=UTF-8")
public ResponseEntity<Void> deleteAll() {
Collection<Post> allPosts = postService.getAllPosts();
allPosts.forEach(post -> postService.delete(post.getId()));
return ResponseEntity.accepted().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@
import java.util.Collection;
import java.util.Optional;

@RequiredArgsConstructor
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

public static final String UNABLE_TO_FIND_USER = "Unable to find user";
private final UserRepository repository;

@GetMapping
public Collection<User> getUsers() {
return repository.findAll();
}

@PostMapping(consumes = "application/json;charset=UTF-8")
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
Optional<User> oldUser = repository.findById(user.getEmail());
Expand Down Expand Up @@ -66,7 +61,6 @@ public ResponseEntity<User> login(LoginRequest request) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, UNABLE_TO_FIND_USER);
}


@DeleteMapping("/{email}")
public ResponseEntity<String> deleteUser(@PathVariable String email) {
repository.deleteById(email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/project/open_hands/util/ImageUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class ImageUtility {
public final class ImageUtility {
private ImageUtility() {
}

public static byte[] compressImage(byte[] data) {

Expand All @@ -21,7 +23,7 @@ public static byte[] compressImage(byte[] data) {
}
try {
outputStream.close();
} catch (Exception e) {
} catch (Exception ignored) {
}
return outputStream.toByteArray();
}
Expand All @@ -37,7 +39,7 @@ public static byte[] decompressImage(byte[] data) {
outputStream.write(tmp, 0, count);
}
outputStream.close();
} catch (Exception exception) {
} catch (Exception ignored) {
}
return outputStream.toByteArray();
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ spring.jpa.properties.hibernate.format_sql=true

#spring.data.jpa.repositories.enabled=true


#This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance fields aren't final
This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance fields aren't final

spring.datasource.initialization-mode=always
spring.jackson.serialization.fail-on-empty-beans=false
spring.http.multipart.max-file-size=10MB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class TestDemoApplication {
@Bean
@ServiceConnection
MySQLContainer<?> mysqlContainer() {
return new MySQLContainer<>("mysql:latest");
return new MySQLContainer<>("mysql:latest")
.withDatabaseName("open_hands_test");
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.project.open_hands.repository;

import com.project.open_hands.Constants;
import com.project.open_hands.entity.Message;
import com.project.open_hands.entity.User;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -29,7 +30,7 @@ void findByFromEmail() {
entity.setTelephoneNo("+4919212221222");
entity.setMessageText("");
entity.setId("");
entity.setRequestTime(LocalDateTime.parse(""));
entity.setRequestTime(LocalDateTime.parse("2023-09-09 17:08:36.163538", Constants.dateTimeFormatter));

Message message = repository.save(entity);
assertThat(message).isNotNull();
Expand Down
Loading

0 comments on commit 4e26f31

Please sign in to comment.