feat: add profile operations

This commit is contained in:
Sergey Krylov 2026-01-29 05:23:56 +03:00
parent 2e4fadab0d
commit 4cc5696788
6 changed files with 206 additions and 8 deletions

View File

@ -0,0 +1,27 @@
package ru.charm.back;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class CharmBackClientRunner {
public static void main(String[] args) throws IOException {
try (
Socket socket = new Socket("localhost", 8080);
DataInputStream rsStream = new DataInputStream(socket.getInputStream());
DataOutputStream rqStream = new DataOutputStream(socket.getOutputStream());
Scanner scanner = new Scanner(System.in)
) {
while (scanner.hasNextLine()) {
System.out.println("Client: ");
String request = scanner.nextLine();
rqStream.writeUTF(request);
String response = rsStream.readUTF();
System.out.println("Server: " + response);
}
}
}
}

View File

@ -0,0 +1,49 @@
package ru.charm.back;
import ru.charm.back.controller.ProfileController;
import ru.charm.back.dao.ProfileDao;
import ru.charm.back.model.Command;
import ru.charm.back.service.ProfileService;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class CharmBackServerRunner {
public static void main(String[] args) throws IOException {
ProfileController controller = new ProfileController(new ProfileService(new ProfileDao()));
try (
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
DataInputStream rqStream = new DataInputStream(socket.getInputStream());
DataOutputStream rsStream = new DataOutputStream(socket.getOutputStream());
) {
String request = rqStream.readUTF();
String response;
while (!request.equals("stop")) {
if (request.startsWith(Command.SAVE.getPrefix())) {
response = controller.save(request.split("save ")[1]);
} else if (request.startsWith(Command.FIND_BY_ID.getPrefix())) {
response = controller.findById(request.split("findById ")[1]);
} else if (request.startsWith(Command.FIND_ALL.getPrefix())) {
response = controller.findAll();
} else if (request.startsWith(Command.UPDATE.getPrefix())) {
response = controller.update(request.split("update ")[1]);
} else if (request.startsWith(Command.DELETE.getPrefix())) {
response = controller.delete(request.split("delete ")[1]);
} else {
response = "Unsupported method";
}
rsStream.writeUTF(response);
request = rqStream.readUTF();
System.out.println("Server: " + request);
}
}
}
}

View File

@ -1,7 +0,0 @@
package ru.charm.back;
public class Main {
public static void main(String[] args) {
System.out.println("Hello from charm");
}
}

View File

@ -1,7 +1,10 @@
package ru.charm.back.controller; package ru.charm.back.controller;
import ru.charm.back.model.Profile;
import ru.charm.back.service.ProfileService; import ru.charm.back.service.ProfileService;
import java.util.Optional;
public class ProfileController { public class ProfileController {
private final ProfileService service; private final ProfileService service;
@ -9,5 +12,101 @@ public class ProfileController {
this.service = service; this.service = service;
} }
public String work(String request) {} public String work(String request) {
return "";
}
public String save(String save) {
String[] params = save.split(",");
if (params.length != 4) {
return "Bad request";
}
Profile profile = new Profile();
profile.setEmail(params[0]);
profile.setName(params[1]);
profile.setSurname(params[2]);
profile.setAbout(params[3]);
return service.save(profile).toString();
}
public String findById(String request) {
String[] params = request.split(",");
if (params.length != 1) {
return "Bad request";
}
long id;
try {
id = Long.parseLong(params[0]);
} catch (NumberFormatException e) {
return "Bad request";
}
Optional<Profile> maybeProfile = service.findById(id);
if (maybeProfile.isEmpty()) {
return "Not found";
}
return maybeProfile.get().toString();
}
public String findAll() {
return service.findAll().toString();
}
public String update(String request) {
String[] params = request.split(",");
if (params.length != 5) {
return "Bad request";
}
long id;
try {
id = Long.parseLong(params[0]);
} catch (NumberFormatException e) {
return "Bad request";
}
Profile profile = new Profile();
profile.setId(id);
profile.setEmail(params[1]);
profile.setName(params[2]);
profile.setSurname(params[3]);
profile.setAbout(params[4]);
service.update(profile);
return "Update success\n" + profile.toString();
}
public String delete(String request) {
String[] params = request.split(",");
if (params.length != 1) {
return "Bad request";
}
long id;
try {
id = Long.parseLong(params[0]);
} catch (NumberFormatException e) {
return "Bad request";
}
Optional<Profile> maybeProfile = service.findById(id);
if (maybeProfile.isEmpty()) {
return "Not found";
}
service.delete(id);
return "Delete success";
}
} }

View File

@ -0,0 +1,19 @@
package ru.charm.back.model;
public enum Command {
SAVE("save"),
FIND_BY_ID("findById"),
FIND_ALL("findAll"),
UPDATE("update"),
DELETE("delete");
private final String prefix;
Command(String prefix) {
this.prefix = prefix;
}
public String getPrefix() {
return prefix;
}
}

View File

@ -46,4 +46,15 @@ public class Profile {
public void setAbout(String about) { public void setAbout(String about) {
this.about = about; this.about = about;
} }
@Override
public String toString() {
return "Profile{" +
"id=" + id +
", email='" + email + '\'' +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
", about='" + about + '\'' +
'}';
}
} }