본문 바로가기
Study/WEB

[Spring Boot With Kotlin] RESTFul API, JSON 데이터 처리하기

by Becoming a Hacker 2022. 7. 24.
반응형

Data Entity

package com.hacksms.AssetsManaged.Model

import java.time.LocalDate
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.ManyToOne

@Entity
class Users(
    @Column(nullable=false, unique=true)
    var loginId: String,
    var password: String,
    var permission: Int, // 1 is admin user, 2 is normal user
    @Id
    @GeneratedValue
    // private Key
    var id: Long? = null
)

@Entity
class Assets(
    @ManyToOne
    private var user: Users,
    var assetType: String,
    var assetValue: Long,
    @Id
    @GeneratedValue
    // private Key
    var id: Long? = null
)

@Entity
class householdLedger(
    @ManyToOne
    private var user: Users,
    var date : LocalDate,
    var consumptionType: String,
    var consumptionValue: Long,
    @Id
    @GeneratedValue
    // private Key
    var id: Long? = null
)

 

Repository

package com.hacksms.AssetsManaged.Model

import org.springframework.data.repository.CrudRepository

interface UsersRepository: CrudRepository<Users, Long>{
    fun findByLoginIdAndPassword(loginId: String, password: String): Users?
}

interface AssetsRepository: CrudRepository<Assets, Long>{
    fun findAllByUser(user: Users): Iterable<Assets>?
}

interface householdLedgerRepository: CrudRepository<householdLedger, Long>{
    fun findAllByUser(user: Users): Iterable<householdLedger>?
}

 

RestFul API DTO

package com.hacksms.AssetsManaged.Dto

data class UsersDto(
    val loginId: String,
    val password: String
)

 

RestFul API Controller

package com.hacksms.AssetsManaged.Controller

import com.hacksms.AssetsManaged.Dto.UsersDto
import com.hacksms.AssetsManaged.Model.UsersRepository
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 javax.servlet.http.HttpSession

@RestController
@RequestMapping("/api")
class RestController(private val usersRepository: UsersRepository) {
    @PostMapping("/login")
    fun login(@RequestBody loginData: UsersDto, session: HttpSession): Any{
        val result = usersRepository.findByLoginIdAndPassword(loginData.loginId, loginData.password)
        if(result==null){
            return "{\"result\":\"ID or Password incorrect\"}"
        }
        session.setAttribute("loginId",loginData.loginId)
        session.setAttribute("permission",result.permission)
        return "{\"result\":\"OK\"}"
    }
}

 

FrontEnd (Thymeleaf)

<!DOCTYPE html>
<html class="h-100">

<div th:replace="fragments/pageLayout :: fragment-head"></div>

<body class="h-100">

<div th:replace="fragments/pageLayout :: fragment-preloader"></div>

<script>
    function loginProcess(){
        $.ajax({
            type: "POST",
            url: "/api/login",
            contentType : "application/json",
            dataType: "json",
            data: JSON.stringify({"loginId":$("#loginId").val(),"password":$("#password").val()}),
            success:function(data){
                if(data.result=="OK"){
                    location.href="/main";
                }else{
                    alert(data.result);
                }
            }
        });
    }
</script>

<div class="login-form-bg h-100">
    <div class="container h-100">
        <div class="row justify-content-center h-100">
            <div class="col-xl-6">
                <div class="form-input-content">
                    <div class="card login-form mb-0">
                        <div class="card-body pt-5">
                            <h4 class="text-center">Login</h4></a>
                            <form class="mt-5 mb-5 login-input" onsubmit="return false">
                                <div class="form-group">
                                    <input type="text" class="form-control" placeholder="Login ID" name="loginId" id="loginId">
                                </div>
                                <div class="form-group">
                                    <input type="password" class="form-control" placeholder="Password" name="password" id="password">
                                </div>
                                <input type="submit" class="btn login-form__btn submit w-100" value="Sign In" onclick="loginProcess()">
                            </form>
                            <p class="mt-5 login-form__footer">Dont have account? <a href="page-register.html" class="text-primary">Sign Up</a> now</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

    <script src="plugins/common/common.min.js"></script>
    <script src="js/custom.min.js"></script>
    <script src="js/settings.js"></script>
    <script src="js/gleek.js"></script>
    <script src="js/styleSwitcher.js"></script>
</body>
</html>

 

댓글