Skip to content

Commit

Permalink
[simba/#47] feat:: API 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEK0111 committed Jun 24, 2024
1 parent 1f4c3c2 commit 7b01626
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 11 deletions.
14 changes: 14 additions & 0 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ dependencies {
implementation("androidx.room:room-runtime:2.6.0")
kapt("androidx.room:room-compiler:2.6.0")

// Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2")
implementation("com.squareup.retrofit2:adapter-rxjava2:2.9.0")

// okHttp
implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")

// Glide
implementation("com.github.bumptech.glide:glide:4.12.0")
annotationProcessor("com.github.bumptech.glide:compiler:4.12.0")

androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.umc_6th

class ApiRepository {
companion object{
const val BASE_URL = "https://edu-api-test.softsquared.com"
}
}
10 changes: 10 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AuthApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.umc_6th

import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST

interface AuthApi {
@POST("/users")
fun signUp(@Body user : User) : Call<BaseResponse>
}
7 changes: 7 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/BaseResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.umc_6th

data class BaseResponse(
val isSuccess : Boolean,
val code : Int,
val message : String
)
16 changes: 16 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/RetrofitInstance.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.umc_6th

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitInstance {
companion object{
private val retrofit by lazy{
Retrofit.Builder()
.baseUrl(ApiRepository.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val authApi = retrofit.create(AuthApi::class.java)
}
}
43 changes: 33 additions & 10 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package com.example.umc_6th

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.ActivitySignupBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SignUpActivity : AppCompatActivity(){

Expand All @@ -23,27 +27,46 @@ class SignUpActivity : AppCompatActivity(){
private fun getUser() : User{
val email : String = binding.signUpIdEt.text.toString() + "@" + binding.signUpDirectInputEt.text.toString()
val pwd : String = binding.signUpPasswordEt.text.toString()
val name: String = binding.signUpNameEt.text.toString()

return User(email, pwd)
return User(email, pwd, name)
}

private fun signUp() {
private fun signUp() : Boolean{
if (binding.signUpIdEt.text.toString().isEmpty() || binding.signUpDirectInputEt.text.toString().isEmpty()){
Toast.makeText(this,"이메일 형식이 잘못되었습니다.",Toast.LENGTH_SHORT).show()
return
return false
}
if (binding.signUpPasswordEt.text.toString() != binding.signUpPasswordCheckEt.text.toString()){
Toast.makeText(this,"비밀번호가 일치하지 않습니다",Toast.LENGTH_SHORT).show()
return
return false
}

val userDB = SongDatabase.getInstance(this)!!
userDB.userDao().insert(getUser())
if(binding.signUpNameEt.text.toString().isEmpty()){
Toast.makeText(this, "이름 형식이 잘못되었습니다.", Toast.LENGTH_SHORT).show()
return false
}

RetrofitInstance.authApi.signUp(getUser()).enqueue(object: Callback<BaseResponse> {
override fun onResponse(call: Call<BaseResponse>, response: Response<BaseResponse>) {
Log.d("SignUp-Success", response.toString())
val response : BaseResponse = response.body()!!
when(response.code) {
1000 -> finish() // 성공
2016, 2018 -> {
binding.signUpEmailErrorTv.visibility = View.VISIBLE
binding.signUpEmailErrorTv.text = response.message
}
}

}

val user = userDB.userDao().getUsers()
Log.d("Sign-up",user.toString())
override fun onFailure(call: Call<BaseResponse>, t: Throwable) {
Log.d("SignUp-Failure", t.message.toString())
}
})
Log.d("SignUpActivity", "All Finished")

Toast.makeText(this,"회원가입이 완료되었습니다.",Toast.LENGTH_SHORT).show()
return
return true
}
}
10 changes: 9 additions & 1 deletion UMC_6th/app/src/main/java/com/example/umc_6th/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ package com.example.umc_6th

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName
import java.io.Serial

@Entity(tableName = "UserTable")
data class User(
@SerializedName("email")
var email : String,
var password : String

@SerializedName("password")
var password : String,

@SerializedName("name")
var name : String
){
@PrimaryKey(autoGenerate = true) var id : Int = 0
}

0 comments on commit 7b01626

Please sign in to comment.