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

Feature/nyh365 step1 #25

Open
wants to merge 17 commits into
base: base/nyh365
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.commons:commons-lang3:3.13.0'
implementation 'org.apache.commons:commons-collections4:4.4'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'com.mysql:mysql-connector-j'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class AssignmentApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.c4marathon.assignment.controller;

import org.c4marathon.assignment.dto.request.PostMainAccountReq;
import org.c4marathon.assignment.dto.request.PostSavingsAccountReq;
import org.c4marathon.assignment.dto.request.WithdrawMainAccountReq;
import org.c4marathon.assignment.dto.response.MainAccountInfoRes;
import org.c4marathon.assignment.dto.response.WithdrawInfoRes;
import org.c4marathon.assignment.service.AccountService;
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 jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/v1/accounts")
@RequiredArgsConstructor
public class AccountController {
private final AccountService accountService;

@PostMapping("/savings")
public void createSavingsAccount(@RequestBody @Valid PostSavingsAccountReq postSavingsAccountReq) {
accountService.createSavingsAccount(postSavingsAccountReq);
}
@PostMapping("/main/deposit")
public MainAccountInfoRes depositMainAccount(@RequestBody @Valid PostMainAccountReq postMainAccountReq) {
return accountService.depositMainAccount(postMainAccountReq);
}
@PostMapping("/savings/withdraw")
public WithdrawInfoRes withdrawForSavings(@RequestBody @Valid WithdrawMainAccountReq withdrawMainAccountReq) {
return accountService.withdrawForSavings(withdrawMainAccountReq);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.c4marathon.assignment.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.c4marathon.assignment.dto.request.PostUserReq;
import org.c4marathon.assignment.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;

@RestController
@RequestMapping("/v1/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;

@PostMapping()
public ResponseEntity<Void> registerUser(@RequestBody @Valid PostUserReq postUserReq) {
userService.registerUser(postUserReq);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.c4marathon.assignment.dto.request;

import jakarta.validation.constraints.Positive;

public record PostMainAccountReq(
@Positive(message = "회원 번호는 양수가 되어야 합니다.")
long userId,

@Positive(message = "이체 금액은 양수가 되어야 합니다.")
int amount
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.c4marathon.assignment.dto.request;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

public record PostSavingsAccountReq(
@Email
@NotBlank
String email
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.c4marathon.assignment.dto.request;

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

public record PostUserReq(
@Size(max = 100)
@NotBlank
String username,

@Email
@NotBlank
String email,

@Size(max = 100)
@NotBlank
String nickname
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.c4marathon.assignment.dto.request;

import jakarta.validation.constraints.Positive;

public record WithdrawMainAccountReq(
@Positive(message = "회원 번호는 양수가 되어야 합니다.")
long userId,
@Positive(message = "적금 계좌번호는 양수가 되어야 합니다.")
long savingsAccount,
@Positive(message = " 금액은 양수가 되어야 합니다.")
int amount
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.c4marathon.assignment.dto.response;

import java.time.Instant;

import org.c4marathon.assignment.entity.Account;

public record MainAccountInfoRes(
Instant depositDate,
Long balance
) {
public MainAccountInfoRes(Account account) {
this(
account.getUpdatedDate(),
account.getBalance()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.c4marathon.assignment.dto.response;

import java.time.Instant;

import org.c4marathon.assignment.entity.Account;

public record WithdrawInfoRes(
Instant withdrawDate,
Long mainAccountBalance,
Long savingsAccountBalance
) {
public WithdrawInfoRes(long mainAccountBalance, long savingsAccountBalance) {
this(
Instant.now(),
mainAccountBalance,
savingsAccountBalance
);
}
}
83 changes: 83 additions & 0 deletions src/main/java/org/c4marathon/assignment/entity/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.c4marathon.assignment.entity;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Setter
@Entity
@Table(name = "account")
public class Account extends BaseEntity {
private static final int DEFAULT_CHARGE_LIMIT = 3_000_000;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "account_id", nullable = false)
private Long id;

@NotNull
@Column(name = "user_id", nullable = false)
private Long userId;

@NotNull
@ColumnDefault("0")
@Column(name = "balance", nullable = false)
private Long balance;

@Column(name = "daily_limit", nullable = false)
private Integer dailyLimit;

@Column(name = "daily_charged_amount", nullable = false)
private Integer dailyChargeAmount ;

@Column(name = "daily_charged_amount_updated_date", nullable = false)
private Instant dailyChargeAmountUpdatedDate;

public Account(Long userId) {
this.userId = userId;
this.balance = 0L;
this.dailyLimit = DEFAULT_CHARGE_LIMIT;
this.dailyChargeAmount = 0;
this.dailyChargeAmountUpdatedDate = Instant.now();
}

public boolean isDailyLimitExceeded(int amount) {
LocalDate updatedDate = this.dailyChargeAmountUpdatedDate.atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate now = Instant.now().atZone(ZoneId.systemDefault()).toLocalDate();

if(updatedDate.isBefore(now)) {
this.dailyChargeAmount = 0;
}

return this.dailyLimit < dailyChargeAmount + amount ? true : false;
}

public void deposit(int amount) {
this.dailyChargeAmount += amount;
this.balance += amount;
}

public boolean isBalanceInsufficient(int amount) {
return this.balance < amount ? true : false;
}

public void withdraw(int amount) {
this.balance -= amount;
}
}
24 changes: 24 additions & 0 deletions src/main/java/org/c4marathon/assignment/entity/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.c4marathon.assignment.entity;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.Instant;

@Getter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class BaseEntity {
@CreatedDate
@Column(updatable = false, nullable = false)
private Instant createDate;

@LastModifiedDate
@Column(nullable = false)
private Instant updatedDate;
}
45 changes: 45 additions & 0 deletions src/main/java/org/c4marathon/assignment/entity/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.c4marathon.assignment.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Column;
import jakarta.persistence.GenerationType;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.hibernate.annotations.ColumnDefault;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Setter
@Entity
@Table(name = "savings_account")
public class SavingsAccount extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "account_id", nullable = false)
private Long id;

@NotNull
@Column(name = "user_id", nullable = false)
private Long userId;

@NotNull
@ColumnDefault("0")
@Column(name = "balance", nullable = false)
private Long balance;

public SavingsAccount(Long userId) {
this.userId = userId;
this.balance = 0L;
}

public void deposit(int amount) {
this.balance += amount;
}
}
49 changes: 49 additions & 0 deletions src/main/java/org/c4marathon/assignment/entity/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.c4marathon.assignment.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import org.springframework.boot.autoconfigure.web.WebProperties;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Setter
@Entity
@Table(name = "user")
public class User extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false)
private Long id;

@Size(max = 30)
@NotNull
@Column(name = "username", nullable = false, length = 30)
private String username;

@Size(max = 30)
@NotNull
@Column(name = "email", nullable = false, length = 30)
private String email;

@Size(max = 10)
@NotNull
@Column(name = "nickname", nullable = false, length = 10)
private String nickname;

@Column(name = "main_account")
private Long mainAccount;

@Builder
public User(String username, String email, String nickname) {
this.username = username;
this.email = email;
this.nickname = nickname;
}

public void changeMainAccount(Long mainAccount) {
this.mainAccount = mainAccount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.c4marathon.assignment.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class CustomException extends RuntimeException {
private final ErrorCode errorCode;
}
Loading
Loading