Skip to content

Commit

Permalink
Code cleanup (#46)
Browse files Browse the repository at this point in the history
* Changed Put and Post mapping

* Added Tests

* Added Tests

* Cleanup

* PR fixes

* Added login integration test

* Removed Log

* Fixed Test

* Added constants

* ID to Id

* Added constants

* Cleanup

* Remove payment series when category is being deleted

* Cleanup

* Changed hover color

Co-authored-by: Tizian <[email protected]>
  • Loading branch information
RedStylzZ and Tizian authored Feb 24, 2022
1 parent 1b81fb1 commit f88e4f7
Show file tree
Hide file tree
Showing 46 changed files with 380 additions and 279 deletions.
8 changes: 6 additions & 2 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
Expand Down Expand Up @@ -69,12 +73,12 @@
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.4</version>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.2.0</version>
<version>4.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
@RequiredArgsConstructor
public class CategoryController {
private static final Log LOG = LogFactory.getLog(CategoryController.class);
private static final String ID_NOT_GIVEN = "Id is not given";
private static final String NAME_NOT_GIVEN = "Name is not given";
private final CategoryService service;
private final MongoUserService userService;

Expand All @@ -43,8 +45,8 @@ public List<CategoryDTO> getCategories(UsernamePasswordAuthenticationToken princ
public List<CategoryDTO> addCategory(UsernamePasswordAuthenticationToken principal, @RequestBody CategoryNameDTO dto) throws ResponseStatusException {
String name = dto.getCategoryName();
if (name == null || name.isBlank()) {
LOG.warn("Name is null or blank");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "No name given");
LOG.warn(NAME_NOT_GIVEN);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, NAME_NOT_GIVEN);
}

MongoUser user = getUser(principal);
Expand All @@ -57,25 +59,29 @@ public List<CategoryDTO> addCategory(UsernamePasswordAuthenticationToken princip

@PatchMapping
public List<CategoryDTO> renameCategory(UsernamePasswordAuthenticationToken principal, @RequestBody CategoryDTO dto) throws ResponseStatusException {
String id = dto.getCategoryID();
String id = dto.getCategoryId();
String name = dto.getCategoryName();
if ((id == null || id.isBlank()) || (name == null || name.isBlank())) {
LOG.warn("ID or name is null or blank");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
if (id == null || id.isBlank()) {
LOG.warn(ID_NOT_GIVEN);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, ID_NOT_GIVEN);
}
if (name == null || name.isBlank()) {
LOG.warn(NAME_NOT_GIVEN);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, NAME_NOT_GIVEN);
}
MongoUser user = userService.getUserByPrincipal(principal);
return service.renameCategory(user, id, name);
}

@DeleteMapping
public List<CategoryDTO> deleteCategory(UsernamePasswordAuthenticationToken principal, @RequestParam String categoryID) throws ResponseStatusException {
if (categoryID == null || categoryID.isBlank()) {
LOG.warn("ID is null or blank");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
public List<CategoryDTO> deleteCategory(UsernamePasswordAuthenticationToken principal, @RequestParam String categoryId) throws ResponseStatusException {
if (categoryId == null || categoryId.isBlank()) {
LOG.warn(ID_NOT_GIVEN);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, ID_NOT_GIVEN);
}

MongoUser user = userService.getUserByPrincipal(principal);
return service.deleteCategory(user, categoryID);
return service.deleteCategory(user, categoryId);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import lombok.RequiredArgsConstructor;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController
@RequestMapping("auth/login")
Expand All @@ -26,8 +28,8 @@ public String login(@RequestBody LoginDTO dto) {
return service.login(mongoUser);
} catch (Exception e) {
LOG.error("Failed to login user", e);
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Wrong Credentials");
}
return null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ private List<PaymentDTO> getPaymentsAsDTO(List<Payment> payments) {

@GetMapping("/")
public PaymentDTO getPayment(UsernamePasswordAuthenticationToken principal,
@RequestParam String categoryID,
@RequestParam String paymentID) {
@RequestParam String categoryId,
@RequestParam String paymentId) {
MongoUser user = getUser(principal);
return Payment.convertPaymentToDTO(service.getPayment(user.getId(), categoryID, paymentID));
return Payment.convertPaymentToDTO(service.getPayment(user.getId(), categoryId, paymentId));
}

@GetMapping("{categoryID}")
@GetMapping("{categoryId}")
public List<PaymentDTO> getPayments(UsernamePasswordAuthenticationToken principal,
@PathVariable String categoryID) {
@PathVariable String categoryId) {
MongoUser user = getUser(principal);
return getPaymentsAsDTO(service.getPayments(user.getId(), categoryID));
return getPaymentsAsDTO(service.getPayments(user.getId(), categoryId));
}

@GetMapping
Expand All @@ -60,25 +60,25 @@ public List<PaymentDTO> getLastPayments(UsernamePasswordAuthenticationToken prin
@PostMapping
public List<PaymentDTO> addPayment(UsernamePasswordAuthenticationToken principal,
@RequestBody RequestPaymentDTO dto) {
String userID = getUser(principal).getId();
String userId = getUser(principal).getId();
Payment payment = Payment.convertDTOtoPayment(dto);
return getPaymentsAsDTO(service.addPayment(userID, payment));
return getPaymentsAsDTO(service.addPayment(userId, payment));
}

@DeleteMapping
public List<PaymentDTO> deletePayment(UsernamePasswordAuthenticationToken principal,
@RequestParam String categoryID,
@RequestParam String paymentID) {
String userID = getUser(principal).getId();
return getPaymentsAsDTO(service.deletePayment(userID, categoryID, paymentID));
@RequestParam String categoryId,
@RequestParam String paymentId) {
String userId = getUser(principal).getId();
return getPaymentsAsDTO(service.deletePayment(userId, categoryId, paymentId));
}

@PutMapping
public List<PaymentDTO> changePayment(UsernamePasswordAuthenticationToken principal,
@RequestBody PaymentDTO dto) {
String userID = getUser(principal).getId();
String userId = getUser(principal).getId();
Payment payment = Payment.convertDTOtoPayment(dto);
return getPaymentsAsDTO(service.changePayment(userID, payment));
return getPaymentsAsDTO(service.changePayment(userId, payment));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ public class CategoryAlreadyExistException extends RuntimeException {
public CategoryAlreadyExistException(String message) {
super(message);
}

public CategoryAlreadyExistException() {
super("A category with this name already exists");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public SeriesAlreadyExistException(String message) {
}

public SeriesAlreadyExistException() {
super();
super("Series already existent");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.redstylzz.backend.exception;

public class SeriesDoesNotExistException extends RuntimeException {
public SeriesDoesNotExistException(String message) {
super(message);
}

public SeriesDoesNotExistException() {
super("Series does not exist");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
public class Category {

@Id
String categoryID;
String categoryId;

String userID;
String userId;
String categoryName;
LocalDateTime saveDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
public class Payment {

@Id
String paymentID;
String paymentId;

String userID;
String categoryID;
String userId;
String categoryId;
String description;
BigDecimal amount;
Instant saveDate;
Instant payDate;

public static Payment convertDTOtoPayment(PaymentDTO dto) {
return Payment.builder()
.paymentID(dto.getPaymentID())
.categoryID(dto.getCategoryID())
.paymentId(dto.getPaymentId())
.categoryId(dto.getCategoryId())
.description(dto.getDescription())
.amount(dto.getAmount())
.payDate(dto.getPayDate().toInstant(ZoneOffset.UTC))
Expand All @@ -44,7 +44,7 @@ public static Payment convertDTOtoPayment(PaymentDTO dto) {

public static Payment convertDTOtoPayment(RequestPaymentDTO dto) {
return Payment.builder()
.categoryID(dto.getCategoryID())
.categoryId(dto.getCategoryId())
.description(dto.getDescription())
.amount(dto.getAmount())
.payDate(dto.getPayDate().toInstant(ZoneOffset.UTC))
Expand All @@ -53,8 +53,8 @@ public static Payment convertDTOtoPayment(RequestPaymentDTO dto) {

public static Payment convertDTOtoPayment(PaymentDTO dto, String userId) {
return Payment.builder()
.userID(userId)
.categoryID(dto.getCategoryID())
.userId(userId)
.categoryId(dto.getCategoryId())
.description(dto.getDescription())
.amount(dto.getAmount())
.payDate(dto.getPayDate().toInstant(ZoneOffset.UTC))
Expand All @@ -63,8 +63,8 @@ public static Payment convertDTOtoPayment(PaymentDTO dto, String userId) {

public static Payment convertDTOtoPayment(PaymentDTO dto, String userId, Instant payDate) {
return Payment.builder()
.userID(userId)
.categoryID(dto.getCategoryID())
.userId(userId)
.categoryId(dto.getCategoryId())
.description(dto.getDescription())
.amount(dto.getAmount())
.payDate(payDate)
Expand All @@ -73,8 +73,8 @@ public static Payment convertDTOtoPayment(PaymentDTO dto, String userId, Instant

public static PaymentDTO convertPaymentToDTO(Payment payment) {
return PaymentDTO.builder()
.paymentID(payment.paymentID)
.categoryID(payment.categoryID)
.paymentId(payment.paymentId)
.categoryId(payment.categoryId)
.description(payment.description)
.amount(payment.amount)
.payDate(LocalDateTime.from(payment.payDate.atZone(ZoneId.of("GMT+1"))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
@Builder
public class CategoryDTO {
String categoryName;
String categoryID;
String categoryId;
BigDecimal paymentSum;

@Transient
public static CategoryDTO mapCategoryToDTO(Category category) {
return CategoryDTO.builder()
.categoryID(category.getCategoryID())
.categoryId(category.getCategoryId())
.categoryName(category.getCategoryName())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryIDDTO {
String categoryID;
public class CategoryIdDTO {
String categoryId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
@NoArgsConstructor
@AllArgsConstructor
public class CategoryPaymentInputDTO {
String categoryID;
String paymentID;
String categoryId;
String paymentId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
@Builder
public class PaymentDTO {

String paymentID;
String categoryID;
String paymentId;
String categoryId;
String description;
BigDecimal amount;
LocalDateTime payDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Builder
public class RequestPaymentDTO {

@NonNull String categoryID;
@NonNull String categoryId;
@NonNull String description;
@NonNull BigDecimal amount;
@NonNull LocalDateTime payDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
@Repository
public interface ICategoryRepository extends MongoRepository<Category, String> {

List<Category> findAllByUserID(String userID);
List<Category> findAllByUserId(String userId);

Category findByUserIDAndCategoryID(String userID, String categoryID);
Category findByUserIdAndCategoryId(String userId, String categoryId);

void deleteByCategoryID(String categoryID);
void deleteByCategoryId(String categoryId);

boolean existsByUserIDAndCategoryName(String userID, String categoryName);
boolean existsByUserIdAndCategoryName(String userId, String categoryName);

boolean existsByUserIDAndCategoryID(String userID, String categoryID);
boolean existsByUserIdAndCategoryId(String userId, String categoryId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@
@Repository
public interface IMongoUserRepository extends MongoRepository<MongoUser, String> {
MongoUser findMongoUserByUsername(String username) throws UsernameNotFoundException;

MongoUser findMongoUserById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
import org.springframework.data.mongodb.repository.MongoRepository;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

public interface IPaymentRepository extends MongoRepository<Payment, String> {

List<Payment> getAllByUserIDAndCategoryIDOrderByPayDateDesc(String userID, String categoryID);
List<Payment> getAllByUserIdAndCategoryIdOrderByPayDateDesc(String userId, String categoryId);

List<Payment> getAllByUserIDAndPayDateAfterOrderByPayDateDesc(String userID, Instant date);
List<Payment> getAllByUserIdAndPayDateAfterOrderByPayDateDesc(String userId, Instant date);

List<Payment> getAllByUserIDAndCategoryIDAndPayDateAfterOrderByPayDateDesc(String userID, String categoryID, LocalDateTime date);
List<Payment> getAllByUserIdAndCategoryIdAndPayDateAfterOrderByPayDateDesc(String userId, String categoryId, Instant date);

Payment getByUserIDAndCategoryIDAndPaymentID(String userID, String categoryID, String paymentID);
Payment getByUserIdAndCategoryIdAndPaymentId(String userId, String categoryId, String paymentId);

boolean existsByPaymentID(String paymentID);
boolean existsByPaymentId(String paymentId);

void deleteByPaymentID(String paymentID);
void deleteByPaymentId(String paymentId);

void deleteAllByUserIDAndCategoryID(String userID, String categoryID);
void deleteAllByUserIdAndCategoryId(String userId, String categoryId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface IPaymentSeriesRepository extends MongoRepository<PaymentSeries,

void deleteByUserIdAndSeriesId(String userId, String seriesId);

void deleteAllByUserIdAndPayment_CategoryId(String userId, String categoryId);

boolean existsBySeriesId(String seriesId);

}
Loading

0 comments on commit f88e4f7

Please sign in to comment.