-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
174 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ target | |
1000000000.csv | ||
*.iml | ||
logs | ||
users.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ru.mifi.practice.vol6.model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import ru.mifi.practice.vol6.Security; | ||
|
||
import java.io.Serializable; | ||
|
||
public record User(@SerializedName("username") String username, | ||
@SerializedName("password") String secret) implements Serializable { | ||
|
||
public boolean equalsSecret(String password, Security.Hash hash) { | ||
return secret.equals(hash.hash(password)); | ||
} | ||
} |
15 changes: 12 additions & 3 deletions
15
vol6/src/main/java/ru/mifi/practice/vol6/repository/Repository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
package ru.mifi.practice.vol6.repository; | ||
|
||
import ru.mifi.practice.vol6.Storage; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface Repository<T, I> { | ||
|
||
List<T> findAll(); | ||
|
||
Optional<T> search(I id); | ||
|
||
void store(Storage storage); | ||
interface Mutant<T, I> extends Repository<T, I> { | ||
|
||
void add(T item); | ||
|
||
void delete(I key); | ||
|
||
void addAll(List<T> items); | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
vol6/src/main/java/ru/mifi/practice/vol6/repository/UserRepository.java
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
vol6/src/main/java/ru/mifi/practice/vol6/repository/UserRepositoryInMemory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ru.mifi.practice.vol6.repository; | ||
|
||
import ru.mifi.practice.vol6.model.User; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public final class UserRepositoryInMemory implements Repository.Mutant<User, String> { | ||
private final Map<String, User> users = new HashMap<>(); | ||
|
||
@Override | ||
public Optional<User> search(String id) { | ||
return Optional.ofNullable(users.get(id)); | ||
} | ||
|
||
@Override | ||
public void add(User user) { | ||
if (users.containsKey(user.username())) { | ||
throw new IllegalArgumentException("User already exists"); | ||
} | ||
users.put(user.username(), user); | ||
} | ||
|
||
@Override | ||
public void delete(String key) { | ||
users.remove(key); | ||
} | ||
|
||
@Override | ||
public void addAll(List<User> items) { | ||
items.forEach(this::add); | ||
} | ||
|
||
@Override | ||
public List<User> findAll() { | ||
return new ArrayList<>(users.values()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
vol6/src/main/java/ru/mifi/practice/vol6/storege/FileStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ru.mifi.practice.vol6.storege; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.reflect.TypeToken; | ||
import ru.mifi.practice.vol6.model.User; | ||
import ru.mifi.practice.vol6.repository.Repository; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class FileStorage implements Storage { | ||
private static final Type LIST_OF_USERS = new TypeToken<ArrayList<User>>() { | ||
}.getType(); | ||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
private final Path users = Path.of("users.json"); | ||
|
||
@Override | ||
public void write(Repository.Mutant<User, String> repository) { | ||
try { | ||
Files.writeString(users, gson.toJson(repository.findAll()), | ||
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Repository.Mutant<User, String> read(Repository.Mutant<User, String> repository) { | ||
try { | ||
List<User> list = gson.fromJson(Files.readString(users, StandardCharsets.UTF_8), LIST_OF_USERS); | ||
repository.addAll(list); | ||
} catch (IOException e) { | ||
//Ignore | ||
} | ||
return repository; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
vol6/src/main/java/ru/mifi/practice/vol6/storege/Storage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ru.mifi.practice.vol6.storege; | ||
|
||
import ru.mifi.practice.vol6.model.User; | ||
import ru.mifi.practice.vol6.repository.Repository; | ||
|
||
public interface Storage { | ||
|
||
void write(Repository.Mutant<User, String> repository); | ||
|
||
Repository.Mutant<User, String> read(Repository.Mutant<User, String> repository); | ||
|
||
final class Empty implements Storage { | ||
|
||
@Override | ||
public void write(Repository.Mutant<User, String> repository) { | ||
System.err.println("запись для users не реализована"); | ||
} | ||
|
||
@Override | ||
public Repository.Mutant<User, String> read(Repository.Mutant<User, String> repository) { | ||
System.err.println("загрузка для users не реализована"); | ||
return repository; | ||
} | ||
} | ||
} |