From 042350ca596ddb59324ce241ba2f0b5dbf573e24 Mon Sep 17 00:00:00 2001 From: zyzf Date: Fri, 15 Dec 2023 11:20:18 +0400 Subject: [PATCH] fixed all bugs --- .idea/deploymentTargetDropDown.xml | 15 ++++++- .idea/inspectionProfiles/Project_Default.xml | 41 ------------------- app/src/main/AndroidManifest.xml | 2 +- .../coffeepreorder/api/MyServerService.kt | 16 ++++---- .../api/coffee/RestCoffeeRepository.kt | 4 +- .../coffeepreorder/api/model/UserRemote.kt | 2 +- .../api/user/RestUserRepository.kt | 11 ++--- .../coffeepreorder/database/dao/CartDao.kt | 4 +- .../coffeepreorder/database/dao/UserDao.kt | 2 +- .../database/repository/CoffeeRepository.kt | 4 +- .../repository/OfflineCoffeeRepository.kt | 4 +- .../repository/OfflineUserRepository.kt | 6 +-- .../database/repository/UserRepository.kt | 6 +-- .../coffeepreorder/ui/login/LoginViewModel.kt | 2 +- .../ui/register/RegisterViewModel.kt | 2 +- .../main/res/xml/network_security_config.xml | 2 +- .../yan/user/controller/UserController.java | 3 +- .../yan/user/repository/UserRepository.java | 2 +- .../yan/user/service/UserService.java | 4 +- .../src/main/resources/application.properties | 2 +- 20 files changed, 54 insertions(+), 80 deletions(-) delete mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 0c0c338..b42850d 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -3,7 +3,20 @@ - + + + + + + + + + + + + + + diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 44ca2d9..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 10d59a1..130dd15 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,7 +8,7 @@ 0 collate nocase") fun getAllInCart(): PagingSource - @Query("select sum(coffee.cost) from cart JOIN coffee on coffee.uid = cart.coffee_id and cart.count > 0") + @Query("select sum(coffee.cost * cart.count) from cart JOIN coffee on coffee.uid = cart.coffee_id and cart.count > 0") fun getSumInCart(): Double - @Query("select coffee.cost from cart JOIN coffee on coffee.uid = cart.coffee_id where coffee.uid = :coffeeId") + @Query("select cart.count from cart JOIN coffee on coffee.uid = cart.coffee_id where coffee.uid = :coffeeId") fun getCountForCoffee(coffeeId: Int): Double @Insert diff --git a/app/src/main/java/com/zyzf/coffeepreorder/database/dao/UserDao.kt b/app/src/main/java/com/zyzf/coffeepreorder/database/dao/UserDao.kt index 5c19799..4f9c1e1 100644 --- a/app/src/main/java/com/zyzf/coffeepreorder/database/dao/UserDao.kt +++ b/app/src/main/java/com/zyzf/coffeepreorder/database/dao/UserDao.kt @@ -17,7 +17,7 @@ interface UserDao { fun getAll(): PagingSource @Query("select * from user where login = :login and password = :password") - fun tryLogin(login: String, password: String): User? + suspend fun tryLogin(login: String, password: String): User? @Query("select * from user where uid = :uid") suspend fun getByUid(uid: Int): User? diff --git a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/CoffeeRepository.kt b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/CoffeeRepository.kt index f3d38d9..d31b1b4 100644 --- a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/CoffeeRepository.kt +++ b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/CoffeeRepository.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow interface CoffeeRepository { fun getAllCoffees(): Flow> suspend fun getByUid(uid: Int): Coffee? - fun insert(coffee: Coffee): Long - fun update(coffee: Coffee): Int + suspend fun insert(coffee: Coffee): Long + suspend fun update(coffee: Coffee): Int suspend fun delete(coffee: Coffee) } \ No newline at end of file diff --git a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineCoffeeRepository.kt b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineCoffeeRepository.kt index 2328956..ae9910b 100644 --- a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineCoffeeRepository.kt +++ b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineCoffeeRepository.kt @@ -21,9 +21,9 @@ class OfflineCoffeeRepository(private val coffeeDao: CoffeeDao) : CoffeeReposito fun getAllCoffeesPagingSource(): PagingSource = coffeeDao.getAllCoffees() suspend fun clearCoffees() = coffeeDao.deleteAll() override suspend fun getByUid(uid: Int): Coffee? = coffeeDao.getByUid(uid) - override fun insert(coffee: Coffee): Long = coffeeDao.insert(coffee) + override suspend fun insert(coffee: Coffee): Long = coffeeDao.insert(coffee) suspend fun insertCoffees(coffees: List) = coffeeDao.insert(*coffees.toTypedArray()) - override fun update(coffee: Coffee): Int = coffeeDao.update(coffee) + override suspend fun update(coffee: Coffee): Int = coffeeDao.update(coffee) override suspend fun delete(coffee: Coffee) = coffeeDao.delete(coffee) } \ No newline at end of file diff --git a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineUserRepository.kt b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineUserRepository.kt index 90af3ce..40df1f5 100644 --- a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineUserRepository.kt +++ b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/OfflineUserRepository.kt @@ -20,11 +20,11 @@ class OfflineUserRepository(private val userDao: UserDao) : UserRepository { ).flow fun getAllUserPagingSource(): PagingSource = userDao.getAll() override suspend fun getByUid(uid: Int): User? = userDao.getByUid(uid) - override fun tryLogin(login: String, password: String): User? = userDao.tryLogin(login, password) - override fun insert(user: User): Long = userDao.insert(user) + override suspend fun tryLogin(login: String, password: String): User? = userDao.tryLogin(login, password) + override suspend fun insert(user: User): Long = userDao.insert(user) fun insertUsers(users: List) = userDao.insert(*users.toTypedArray()) - override fun update(user: User): Int? = userDao.update(user) + override suspend fun update(user: User): Int? = userDao.update(user) override suspend fun delete(user: User) = userDao.delete(user) suspend fun clearUsers() = userDao.deleteAll() } \ No newline at end of file diff --git a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/UserRepository.kt b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/UserRepository.kt index 66e314f..a7bf991 100644 --- a/app/src/main/java/com/zyzf/coffeepreorder/database/repository/UserRepository.kt +++ b/app/src/main/java/com/zyzf/coffeepreorder/database/repository/UserRepository.kt @@ -7,8 +7,8 @@ import kotlinx.coroutines.flow.Flow interface UserRepository { fun getAll(): Flow> suspend fun getByUid(uid: Int): User? - fun tryLogin(login: String, password: String): User? - fun insert(user: User): Long - fun update(user: User) : Int? + suspend fun tryLogin(login: String, password: String): User? + suspend fun insert(user: User): Long + suspend fun update(user: User) : Int? suspend fun delete(user: User) } \ No newline at end of file diff --git a/app/src/main/java/com/zyzf/coffeepreorder/ui/login/LoginViewModel.kt b/app/src/main/java/com/zyzf/coffeepreorder/ui/login/LoginViewModel.kt index 76fede0..1b14407 100644 --- a/app/src/main/java/com/zyzf/coffeepreorder/ui/login/LoginViewModel.kt +++ b/app/src/main/java/com/zyzf/coffeepreorder/ui/login/LoginViewModel.kt @@ -8,7 +8,7 @@ import com.zyzf.coffeepreorder.database.repository.UserRepository class LoginViewModel( private val userRepository: UserRepository ) : ViewModel() { - fun tryLogin(login: String, password: String): User? { + suspend fun tryLogin(login: String, password: String): User? { return userRepository.tryLogin(login, password) } } diff --git a/app/src/main/java/com/zyzf/coffeepreorder/ui/register/RegisterViewModel.kt b/app/src/main/java/com/zyzf/coffeepreorder/ui/register/RegisterViewModel.kt index ea28a9e..d0dcf7b 100644 --- a/app/src/main/java/com/zyzf/coffeepreorder/ui/register/RegisterViewModel.kt +++ b/app/src/main/java/com/zyzf/coffeepreorder/ui/register/RegisterViewModel.kt @@ -8,7 +8,7 @@ import com.zyzf.coffeepreorder.database.repository.UserRepository class RegisterViewModel( private val userRepository: UserRepository ) : ViewModel() { - fun register(user: User): User? { + suspend fun register(user: User): User? { userRepository.insert(user) return userRepository.tryLogin(user.login, user.password) } diff --git a/app/src/main/res/xml/network_security_config.xml b/app/src/main/res/xml/network_security_config.xml index d5d65db..5f2cbf3 100644 --- a/app/src/main/res/xml/network_security_config.xml +++ b/app/src/main/res/xml/network_security_config.xml @@ -1,6 +1,6 @@ - 192.168.0.100 + 192.168.42.48 \ No newline at end of file diff --git a/backend/src/main/java/com/kalyshev/yan/user/controller/UserController.java b/backend/src/main/java/com/kalyshev/yan/user/controller/UserController.java index ca39cd2..8ce8cc5 100644 --- a/backend/src/main/java/com/kalyshev/yan/user/controller/UserController.java +++ b/backend/src/main/java/com/kalyshev/yan/user/controller/UserController.java @@ -7,6 +7,7 @@ import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; @RestController @RequestMapping(WebConfiguration.REST_API + "/user") @@ -29,7 +30,7 @@ public class UserController { return userService.findAllUsers(pageNo, pageSize, sortBy, sortDir); } @GetMapping("/tryLogin") - public User tryLogin( + public Optional tryLogin( @RequestParam(value = "login") String login, @RequestParam(value = "password") String password ){ diff --git a/backend/src/main/java/com/kalyshev/yan/user/repository/UserRepository.java b/backend/src/main/java/com/kalyshev/yan/user/repository/UserRepository.java index 5f91f20..6a77934 100644 --- a/backend/src/main/java/com/kalyshev/yan/user/repository/UserRepository.java +++ b/backend/src/main/java/com/kalyshev/yan/user/repository/UserRepository.java @@ -8,7 +8,7 @@ import org.springframework.data.repository.query.Param; import java.util.Optional; public interface UserRepository extends JpaRepository { - @Query(value = "select * from user_u where \"LOGIN\" = :login and \"PASSWORD\" = :password", nativeQuery = true) + @Query(value = "select * from \"USER_U\" where \"LOGIN\" = :login and \"PASSWORD\" = :password", nativeQuery = true) public Optional tryLogin(@Param("login") String login, @Param("password") String password); diff --git a/backend/src/main/java/com/kalyshev/yan/user/service/UserService.java b/backend/src/main/java/com/kalyshev/yan/user/service/UserService.java index 3707a8d..c25a613 100644 --- a/backend/src/main/java/com/kalyshev/yan/user/service/UserService.java +++ b/backend/src/main/java/com/kalyshev/yan/user/service/UserService.java @@ -52,9 +52,9 @@ public class UserService { return user.orElseThrow(() -> new UserNotFoundException(id)); } @Transactional(readOnly = true) - public User tryLogin(String login, String password) { + public Optional tryLogin(String login, String password) { final Optional user = userRepository.tryLogin(login, password); - return user.orElseThrow(() -> new UserNotFoundException((long)0)); + return user; } @Transactional(readOnly = true) public List findAllUsers(int pageNo, int pageSize, String sortBy, String sortDir) { diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index ccc05e8..0cce251 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -8,4 +8,4 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=true spring.h2.console.settings.trace=false -spring.h2.console.settings.web-allow-others=false \ No newline at end of file +spring.h2.console.settings.web-allow-others=true \ No newline at end of file