feat: All entity and UserApi added

This commit is contained in:
SunZar 2025-02-19 15:32:43 +03:00
parent 148df734a9
commit 430e0bfcdf
13 changed files with 481 additions and 3 deletions

View File

@ -0,0 +1,52 @@
package ru.myitschool.work.data;
import androidx.annotation.NonNull;
import java.util.function.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import ru.myitschool.work.domain.entities.Status;
public class CallToConsumer <SOURCE, DEST> implements Callback<SOURCE> {
@NonNull
private final Consumer<Status<DEST>> callback;
@NonNull
private final Mapper<SOURCE, DEST> mapper;
public CallToConsumer(
@NonNull Consumer<Status<DEST>> callback,
@NonNull Mapper<SOURCE, DEST> mapper
) {
this.callback = callback;
this.mapper = mapper;
}
@Override
public void onResponse(@NonNull Call<SOURCE> call, @NonNull Response<SOURCE> response) {
callback.accept(
new Status<>(
response.code(),
mapper.map(response.body()),
null
)
);
}
@Override
public void onFailure(@NonNull Call<SOURCE> call, @NonNull Throwable throwable) {
callback.accept(
new Status<>(
-1,
null,
throwable
)
);
}
public interface Mapper<SOURCE, DEST> {
DEST map(SOURCE source);
}
}

View File

@ -0,0 +1,60 @@
package ru.myitschool.work.data;
import androidx.annotation.NonNull;
import androidx.camera.core.processing.SurfaceProcessorNode;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import ru.myitschool.work.data.dto.HistoryDto;
import ru.myitschool.work.data.dto.UserDto;
import ru.myitschool.work.data.network.RetrofitFactory;
import ru.myitschool.work.data.source.CredentialsDataSource;
import ru.myitschool.work.data.source.UserApi;
import ru.myitschool.work.domain.entities.HistioryEntity;
import ru.myitschool.work.domain.entities.Status;
import ru.myitschool.work.domain.entities.UserEntity;
public class UserResponseImpl {
private static UserResponseImpl INSTANCE;
private UserApi userApi = RetrofitFactory.getInstance().getUserApi();
private final CredentialsDataSource credentialsDataSource = CredentialsDataSource.getInstance();
private UserResponseImpl() {}
public static synchronized UserResponseImpl getInstance() {
if (INSTANCE == null) {
INSTANCE = new UserResponseImpl();
}
return INSTANCE;
}
public void getAllUsers(@NonNull Consumer<Status<List<UserEntity>>> callback) {
userApi.getAllUsers().enqueue(new CallToConsumer<>(
callback,
usersDto -> {
ArrayList<UserEntity> result = new ArrayList<>(usersDto.size());
for (UserDto user : usersDto) {
final String username = user.username;
final String photo = user.photo;
final Long lastVisit = user.lastVisit;
final Integer idUser = user.idUser;
final String position = user.position;
if (idUser != null && username != null) {
result.add(new UserEntity(username, photo, lastVisit, idUser, position));
}
}
return result;
}
));
}
}

View File

@ -1,4 +1,4 @@
package ru.myitschool.work.dto;
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;

View File

@ -0,0 +1,16 @@
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class ReaderDto {
@Nullable
@SerializedName("idReader")
public Integer idReader;
@Nullable
@SerializedName("typeReader")
public Integer typeReader;
}

View File

@ -1,4 +1,4 @@
package ru.myitschool.work.dto;
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;

View File

@ -1,4 +1,4 @@
package ru.myitschool.work.dto;
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;

View File

@ -0,0 +1,48 @@
package ru.myitschool.work.data.network;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.myitschool.work.data.source.CredentialsDataSource;
import ru.myitschool.work.data.source.UserApi;
public class RetrofitFactory {
private static RetrofitFactory INSTANCE;
private RetrofitFactory() {}
public static synchronized RetrofitFactory getInstance() {
if (INSTANCE == null) {
INSTANCE = new RetrofitFactory();
}
return INSTANCE;
}
private final OkHttpClient.Builder client = new OkHttpClient.Builder()
.addInterceptor(chain -> {
String authData = CredentialsDataSource.getInstance().getAuthData();
if (authData == null) {
return chain.proceed(chain.request());
} else {
Request request = chain.request()
.newBuilder()
.addHeader("Authorization", authData)
.build();
return chain.proceed(request);
}
}
);
private final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:8080/")
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
public UserApi getUserApi() {
return retrofit.create(UserApi.class);
}
}

View File

@ -0,0 +1,36 @@
package ru.myitschool.work.data.source;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import okhttp3.Credentials;
public class CredentialsDataSource {
private static CredentialsDataSource INSTANCE;
private CredentialsDataSource() {}
public static synchronized CredentialsDataSource getInstance() {
if (INSTANCE == null) {
INSTANCE = new CredentialsDataSource();
}
return INSTANCE;
}
@Nullable
private String authData = null;
@Nullable
public String getAuthData() {
return authData;
}
public void updateLogin(@NonNull String username, @NonNull String password) {
authData = Credentials.basic(username, password);
}
public void logout() {
authData = null;
}
}

View File

@ -0,0 +1,29 @@
package ru.myitschool.work.data.source;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import ru.myitschool.work.data.dto.HistoryDto;
import ru.myitschool.work.data.dto.UserDto;
import ru.myitschool.work.ui.History;
public interface UserApi {
@GET("api/user")
Call<List<UserDto>> getAllUsers();
@GET("api/user/{id}")
Call<UserDto> getUserById(@Path("id") Integer id);
@GET("api/user/username/{username}")
Call<UserDto> isUserExist(@Path("username") String username);
@GET("api/user/login")
Call<Void> login();
@GET("api/history/user/{id}")
Call<List<History>> getAllUserHistory(@Path("id") Integer id);
@GET("api/history/{id}")
Call<History> getHistoryById(@Path("id") Integer id);
}

View File

@ -0,0 +1,81 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class HistioryEntity {
@Nullable
@SerializedName("id")
public Integer id;
@Nullable
public Integer getId() {
return id;
}
public void setId(@Nullable Integer id) {
this.id = id;
}
@Nullable
public Integer getIdUser() {
return idUser;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
}
@Nullable
public Long getTime() {
return time;
}
public void setTime(@Nullable Long time) {
this.time = time;
}
@Nullable
public String getNameReader() {
return nameReader;
}
public void setNameReader(@Nullable String nameReader) {
this.nameReader = nameReader;
}
@Nullable
public String getType() {
return type;
}
public void setType(@Nullable String type) {
this.type = type;
}
public HistioryEntity(@Nullable Integer id, @Nullable Integer idUser, @Nullable Long time, @Nullable String nameReader, @Nullable String type) {
this.id = id;
this.idUser = idUser;
this.time = time;
this.nameReader = nameReader;
this.type = type;
}
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("time")
public Long time;
@Nullable
@SerializedName("nameReader")
public String nameReader;
@Nullable
@SerializedName("type")
public String type;
}

View File

@ -0,0 +1,37 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class RoleEntity {
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("status")
public int status;
public RoleEntity(@Nullable Integer idUser, int status) {
this.idUser = idUser;
this.status = status;
}
@Nullable
public Integer getIdUser() {
return idUser;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}

View File

@ -0,0 +1,33 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
public class Status<T>{
private final int statusCode;
@Nullable
private final T value;
@Nullable
private final Throwable errors;
public Status(int statusCode, @Nullable T value, @Nullable Throwable errors) {
this.statusCode = statusCode;
this.value = value;
this.errors = errors;
}
public int getStatusCode() {
return statusCode;
}
@Nullable
public T getValue() {
return value;
}
@Nullable
public Throwable getErrors() {
return errors;
}
}

View File

@ -0,0 +1,86 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class UserEntity {
@Nullable
@SerializedName("username")
public String username;
@Nullable
@SerializedName("photo")
public String photo;
@Nullable
@SerializedName("lastVisit")
public Long lastVisit;
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("position")
public String position;
public UserEntity(@Nullable String username, @Nullable String photo, @Nullable Long lastVisit, @Nullable Integer idUser, @Nullable String position) {
this.username = username;
this.photo = photo;
this.lastVisit = lastVisit;
this.idUser = idUser;
this.position = position;
}
@Nullable
public String getPosition() {
return position;
}
public void setPosition(@Nullable String position) {
this.position = position;
}
@Nullable
public String getUsername() {
return username;
}
public void setUsername(@Nullable String username) {
this.username = username;
}
@Nullable
public String getPhoto() {
return photo;
}
public void setPhoto(@Nullable String photo) {
this.photo = photo;
}
@Nullable
public Long getLastVisit() {
return lastVisit;
}
public void setLastVisit(@Nullable Long lastVisit) {
this.lastVisit = lastVisit;
}
@Nullable
public Integer getIdUser() {
return idUser;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
}
}