Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Schedule api (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vita133 authored Mar 5, 2024
1 parent db1a2be commit ca38257
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fictadvisor.android.data.dto.schedule

data class DetailedEventResponse(
val url: String,
val eventInfo: String,
val disciplineType: TDiscipline,
val disciplineInfo: String,
val period: TEventPeriod,
val teachers: List<Teacher>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.fictadvisor.android.data.dto.schedule

data class DisciplineTypeDTO(
val id: String,
val disciplineId: String,
val name: TDiscipline
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fictadvisor.android.data.dto.schedule

data class EventDTO(
val id: String,
val name: String,
val startTime: String,
val endTime: String,
val disciplineType: DisciplineTypeDTO?
)


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.fictadvisor.android.data.dto.schedule

data class GetEventResponse (
val week: String,
val events: List<EventDTO>,
val startTime: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fictadvisor.android.data.dto.schedule

data class PatchEventDTO(
val week: Int,
val changeStartDate: Boolean,
val changeEndDate: Boolean,
val disciplineType: String? = null,
val url: String,
val eventInfo: String,
val disciplineInfo: String,
val period: TEventPeriod,
val teachers: List<String>,
val disciplineId: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.fictadvisor.android.data.dto.schedule

data class PostEventDTO (
val groupId: String,
val teachers: List<String>,
val disciplineId: String,
val url: String,
val eventInfo: String,
val disciplineType: TDiscipline,
val disciplineInfo: String,
val period: TEventPeriod,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fictadvisor.android.data.dto.schedule

enum class TDiscipline {
LECTURE,
PRACTICE,
LABORATORY,
CONSULTATION,
WORKOUT,
EXAM
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.fictadvisor.android.data.dto.schedule

enum class TEventPeriod {
NO_PERIOD,
EVERY_WEEK,
EVERY_FORTNIGHT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.fictadvisor.android.data.dto.schedule

data class Teacher(
val id: String,
val firstName: String,
val middleName: String,
val lastName: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.fictadvisor.android.data.remote

import com.fictadvisor.android.data.remote.api.AuthApi
import com.fictadvisor.android.data.remote.api.GroupApi
import com.fictadvisor.android.data.remote.api.ScheduleApi
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand Down Expand Up @@ -66,4 +67,10 @@ object RetrofitClient {
.create(GroupApi::class.java)
}

val scheduleApi: ScheduleApi by lazy {
retrofitClient
.build()
.create(ScheduleApi::class.java)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.fictadvisor.android.data.remote.api

import com.fictadvisor.android.data.dto.schedule.DetailedEventResponse
import com.fictadvisor.android.data.dto.schedule.GetEventResponse
import com.fictadvisor.android.data.dto.schedule.PatchEventDTO
import com.fictadvisor.android.data.dto.schedule.PostEventDTO
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query

interface ScheduleApi {
@GET("/v2/schedule/groups/{groupId}/general")
suspend fun getEvents(
@Path("groupId") groupId: String,
@Query("week") week: Int,
@Query("addLecture") addLecture: Boolean = true,
@Query("addLaboratory") addLaboratory: Boolean = true,
@Query("addPractice") addPractice: Boolean = true
): Response<GetEventResponse>

@GET("/v2/schedule/groups/{groupId}/events")
suspend fun getEventsAuthorized(
@Path("groupId") groupId: String,
@Query("week") week: Int,
@Query("showOwnSelective") showOwnSelective: Boolean,
@Query("addLecture") addLecture: Boolean = true,
@Query("addLaboratory") addLaboratory: Boolean = true,
@Query("addPractice") addPractice: Boolean = true,
@Query("otherEvents") otherEvents: Boolean = true
): Response<GetEventResponse>

@GET("/v2/schedule/events/{eventId}")
suspend fun getEventInfo(
@Path("eventId") eventId: String,
@Query("week") week: Any // You can define the type accordingly
): Response<DetailedEventResponse>

@DELETE("/v2/schedule/groups/{groupId}/events/{eventId}")
suspend fun deleteEventById(
@Path("groupId") groupId: String,
@Path("eventId") eventId: String
): Response<DetailedEventResponse>

@POST("/v2/schedule/events")
suspend fun addEvent(
@Body body: PostEventDTO,
@Query("groupId") groupId: String
): Response<DetailedEventResponse>

@PATCH("/v2/schedule/groups/{groupId}/events/{eventId}")
suspend fun editEvent(
@Body body: PatchEventDTO,
@Path("groupId") groupId: String,
@Path("eventId") eventId: String
): Response<DetailedEventResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.fictadvisor.android.repository

import com.fictadvisor.android.data.dto.schedule.DetailedEventResponse
import com.fictadvisor.android.data.dto.schedule.GetEventResponse
import com.fictadvisor.android.data.dto.schedule.PatchEventDTO
import com.fictadvisor.android.data.dto.schedule.PostEventDTO
import com.fictadvisor.android.data.remote.RetrofitClient
import retrofit2.Response

class ScheduleRepository {
private val scheduleService = RetrofitClient.scheduleApi

suspend fun getEvents(groupId: String, week: Int): Response<GetEventResponse> {
return scheduleService.getEvents(groupId, week)
}

suspend fun getEventsAuthorized(groupId: String, week: Int, showOwnSelective: Boolean): Response<GetEventResponse> {
return scheduleService.getEventsAuthorized(groupId, week, showOwnSelective)
}

suspend fun getEventInfo(eventId: String, week: Any): Response<DetailedEventResponse> {
return scheduleService.getEventInfo(eventId, week)
}

suspend fun deleteEventById(groupId: String, eventId: String): Response<DetailedEventResponse> {
return scheduleService.deleteEventById(groupId, eventId)
}

suspend fun addEvent(body: PostEventDTO, groupId: String): Response<DetailedEventResponse> {
return scheduleService.addEvent(body, groupId)
}

suspend fun editEvent(body: PatchEventDTO, groupId: String, eventId: String): Response<DetailedEventResponse> {
return scheduleService.editEvent(body, groupId, eventId)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.fictadvisor.android.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.fictadvisor.android.data.dto.BaseResponse
import com.fictadvisor.android.data.dto.ErrorResponse
import com.fictadvisor.android.data.dto.schedule.DetailedEventResponse
import com.fictadvisor.android.data.dto.schedule.GetEventResponse
import com.fictadvisor.android.data.dto.schedule.PatchEventDTO
import com.fictadvisor.android.data.dto.schedule.PostEventDTO
import com.fictadvisor.android.repository.ScheduleRepository
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ScheduleViewModel(private val mainRepository: ScheduleRepository) : ViewModel() {
var job: Job? = null
val mainDispatcher = Dispatchers.Main

private val getEventsResponseMutable = MutableLiveData<BaseResponse<GetEventResponse>>()
val getEventsResponse: LiveData<BaseResponse<GetEventResponse>> = getEventsResponseMutable

private val getEventsAuthorizedResponseMutable = MutableLiveData<BaseResponse<GetEventResponse>>()
val getEventsAuthorizedResponse: LiveData<BaseResponse<GetEventResponse>> = getEventsAuthorizedResponseMutable

private val getEventInfoResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>()
val getEventInfoResponse: LiveData<BaseResponse<DetailedEventResponse>> = getEventInfoResponseMutable

private val deleteEventByIdResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>()
val deleteEventByIdResponse: LiveData<BaseResponse<DetailedEventResponse>> = deleteEventByIdResponseMutable

private val addEventResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>()
val addEventResponse: LiveData<BaseResponse<DetailedEventResponse>> = addEventResponseMutable

private val editEventResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>()
val editEventResponse: LiveData<BaseResponse<DetailedEventResponse>> = editEventResponseMutable

val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
Log.e("ScheduleViewModel", "Exception handled: ${throwable.localizedMessage}")
}

fun getEvents(groupId: String, week: Int){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.getEvents(groupId, week)
withContext(mainDispatcher) {
if (response.isSuccessful) {
getEventsResponseMutable.postValue(BaseResponse.Success(response.body()))
} else {
val gson = Gson()
val type = object : TypeToken<ErrorResponse>() {}.type
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type)

getEventsResponseMutable.postValue(BaseResponse.Error(errorResponse))
}
}
}
}

fun getEventsAuthorized(groupId: String, week: Int, showOwnSelective: Boolean){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.getEventsAuthorized(groupId, week, showOwnSelective)
withContext(mainDispatcher) {
if (response.isSuccessful) {
getEventsAuthorizedResponseMutable.postValue(BaseResponse.Success(response.body()))
} else {
val gson = Gson()
val type = object : TypeToken<ErrorResponse>() {}.type
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type)

getEventsAuthorizedResponseMutable.postValue(BaseResponse.Error(errorResponse))
}
}
}
}

fun getEventInfo(eventId: String, week: Any){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.getEventInfo(eventId, week)
withContext(mainDispatcher) {
if (response.isSuccessful) {
getEventInfoResponseMutable.postValue(BaseResponse.Success(response.body()))
} else {
val gson = Gson()
val type = object : TypeToken<ErrorResponse>() {}.type
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type)

getEventInfoResponseMutable.postValue(BaseResponse.Error(errorResponse))
}
}
}
}

fun deleteEventById(groupId: String, eventId: String){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.deleteEventById(groupId, eventId)
withContext(mainDispatcher) {
if (response.isSuccessful) {
deleteEventByIdResponseMutable.postValue(BaseResponse.Success(response.body()))
} else {
val gson = Gson()
val type = object : TypeToken<ErrorResponse>() {}.type
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type)

deleteEventByIdResponseMutable.postValue(BaseResponse.Error(errorResponse))
}
}
}
}

fun addEvent(body: PostEventDTO, groupId: String){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.addEvent(body, groupId)
withContext(mainDispatcher) {
if (response.isSuccessful) {
addEventResponseMutable.postValue(BaseResponse.Success(response.body()))
} else {
val gson = Gson()
val type = object : TypeToken<ErrorResponse>() {}.type
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type)

addEventResponseMutable.postValue(BaseResponse.Error(errorResponse))
}
}
}
}

fun editEvent(body: PatchEventDTO, groupId: String, eventId: String){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.editEvent(body, groupId, eventId)
withContext(mainDispatcher) {
if (response.isSuccessful) {
editEventResponseMutable.postValue(BaseResponse.Success(response.body()))
} else {
val gson = Gson()
val type = object : TypeToken<ErrorResponse>() {}.type
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type)

editEventResponseMutable.postValue(BaseResponse.Error(errorResponse))
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fictadvisor.android.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.fictadvisor.android.repository.ScheduleRepository

class ScheduleViewModelFactory(private val scheduleRepository: ScheduleRepository) : ViewModelProvider.Factory {

override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(ScheduleViewModel::class.java)) {
return ScheduleViewModel(scheduleRepository) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}

0 comments on commit ca38257

Please sign in to comment.