Skip to content

Commit

Permalink
Day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Naman-Bhalla committed Feb 5, 2022
1 parent d84fa2a commit 19b1894
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,40 @@
import dev.naman.splitwise_feb22.services.command.UpdateProfileCommand;
import dev.naman.splitwise_feb22.services.command.registry.CommandRegistry;
import dev.naman.splitwise_feb22.services.command.registry.CommandRegistryImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
public class SplitwiseFeb22Application {
@EnableJpaAuditing
public class SplitwiseFeb22Application implements CommandLineRunner {

@Autowired
private RegisterUserCommand registerUserCommand;

@Autowired
private UpdateProfileCommand updateProfileCommand;

@Autowired
private CommandRegistry commandRegistry;

public static void main(String[] args) {
SpringApplication.run(SplitwiseFeb22Application.class, args);
}

RegisterUserCommand registerUserCommand = new RegisterUserCommand();
UpdateProfileCommand updateProfileCommand = new UpdateProfileCommand();

CommandRegistry commandRegistry = new CommandRegistryImp();
@Override
public void run(String... args) throws Exception {
commandRegistry.registerCommand(registerUserCommand);
commandRegistry.registerCommand(updateProfileCommand);

String input = "u1 123 123";// read from command line
String input = "Register vinsmokesanji 003 namisswwaann";// read from command line

commandRegistry.executeCommand(input);

input = "2 UpdateProfile robinchwan";

commandRegistry.executeCommand(input);
// while (true) {
Expand All @@ -33,7 +50,6 @@ public static void main(String[] args) {
// and that allows to parse commands to find the correct command out
// of the ones that are registered
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dev.naman.splitwise_feb22.controllers;

import dev.naman.splitwise_feb22.dto.*;
import dev.naman.splitwise_feb22.models.User;
import dev.naman.splitwise_feb22.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Controller
public class UserController {
@Autowired
private UserService userService;

public ResponseDTO<RegisterUserResponseDTO>
registerUser(RegisterUserRequestDTO request) {
User user = userService.registerUser(
request.getName(),
request.getPhoneNumber(),
request.getPassword()
);

ResponseDTO<RegisterUserResponseDTO> response = new ResponseDTO<>();

if (user == null) {
response.setStatusCode(ResponseStatusCode.FAILURE);
return response;
}

response.setStatusCode(ResponseStatusCode.SUCCESS);
RegisterUserResponseDTO responseDTO = new RegisterUserResponseDTO();
responseDTO.setUserId(user.getId());
response.setData(responseDTO);
return response;
}

public ResponseDTO<UpdateProfileResponseDto> updateProfile(
UpdateProfileRequestDto requestDto
) {
userService.updatePassword(requestDto.getUserId(),
requestDto.getNewPassword());
ResponseDTO<UpdateProfileResponseDto> res = new ResponseDTO<>();
res.setStatusCode(ResponseStatusCode.SUCCESS);
return res;
}

}


// controller
// take request
// unmarshall parameters
// send those to a relevant service
// get response from the service
// construct appropriate response object
// secde
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.naman.splitwise_feb22.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class RegisterUserRequestDTO {
private String name;
private String phoneNumber;
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.naman.splitwise_feb22.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class RegisterUserResponseDTO {
private Long userId;
}
20 changes: 20 additions & 0 deletions src/main/java/dev/naman/splitwise_feb22/dto/ResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.naman.splitwise_feb22.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ResponseDTO<T> {
public ResponseStatusCode statusCode;
public T data;
}


// ArrayList<Animal>
// HashMap<Animal, Long>

// class HashMap<T, X> {
// private List<T> keys;
// private List<X> values;
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.naman.splitwise_feb22.dto;

public enum ResponseStatusCode {
SUCCESS,
FAILURE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.naman.splitwise_feb22.dto;


import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Service;

@Getter
@Setter
public class UpdateProfileRequestDto {
private Long userId;
private String newPassword;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.naman.splitwise_feb22.dto;

public class UpdateProfileResponseDto {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.naman.splitwise_feb22.repositories;

import dev.naman.splitwise_feb22.models.User;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

User save(User user);

Optional<User> findById(Long id);
// select * from users where name == ""
// order by name asc;

}
48 changes: 48 additions & 0 deletions src/main/java/dev/naman/splitwise_feb22/services/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.naman.splitwise_feb22.services;

import dev.naman.splitwise_feb22.models.User;
import dev.naman.splitwise_feb22.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserService {
@Autowired
UserRepository userRepository;

public User registerUser(String name,
String phoneNumber,
String password) {
User user = new User();
user.setName(name);
user.setHashedPassword(password);
user.setPhoneNumber(phoneNumber);

User userResponse = userRepository.save(user);

return userResponse;
}

public boolean updatePassword(Long userId,
String newPassword) {
// 1. find the user with the user id
// 2. update the password of the user
// 3. save the new user
Optional<User> user = userRepository.findById(userId);

System.out.println("Found a user");

if (user.isEmpty()) {
// TODO: Throw exception
return false;
}

User userObj = user.get();
userObj.setHashedPassword(newPassword);
userRepository.save(userObj);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package dev.naman.splitwise_feb22.services.command;

import dev.naman.splitwise_feb22.controllers.UserController;
import dev.naman.splitwise_feb22.dto.RegisterUserRequestDTO;
import dev.naman.splitwise_feb22.dto.RegisterUserResponseDTO;
import dev.naman.splitwise_feb22.dto.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Component
public class RegisterUserCommand implements Command {
@Autowired
private UserController userController;

@Override
public boolean matches(String input) {
Expand All @@ -23,7 +32,29 @@ public boolean matches(String input) {

@Override
public void execute(String input) {
List<String> inputList = Arrays.stream(input.split(" ")).toList();
String username = inputList.get(1);
String phoneNumber = inputList.get(2);
String password = inputList.get(3);

RegisterUserRequestDTO registerUserRequestDTO = new RegisterUserRequestDTO();
registerUserRequestDTO.setName(username);
registerUserRequestDTO.setPassword(password);
registerUserRequestDTO.setPhoneNumber(phoneNumber);

ResponseDTO<RegisterUserResponseDTO> response = userController.
registerUser(registerUserRequestDTO);
System.out.println("Registering User");
}

}

// Dependency Inversion= D of SOLID.
// No 2 classes should vbe directly dependent on each other. THey should depend
// on each other via an abstraction layer


// Dependecy Injection
// The dependencies of a class are not created by the class, but passed t the class via constructor
// Inversion of Control
// Framework will create the dependencies and wirew them into the concrete objects;
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package dev.naman.splitwise_feb22.services.command;

import dev.naman.splitwise_feb22.controllers.UserController;
import dev.naman.splitwise_feb22.dto.UpdateProfileRequestDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Component
public class UpdateProfileCommand implements Command {
private static final int size = 3;

@Autowired
private UserController userController;

@Override
public void execute(String input) {
List<String> inputList = Arrays.stream(input.split(" ")).toList();

Long userId = Long.parseLong(inputList.get(0));
String newPassword = inputList.get(2);

UpdateProfileRequestDto updateProfileRequestDto = new UpdateProfileRequestDto();
updateProfileRequestDto.setNewPassword(newPassword);
updateProfileRequestDto.setUserId(userId);

userController.updateProfile(updateProfileRequestDto);

System.out.println("Updated User");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.naman.splitwise_feb22.services.command.registry;

import dev.naman.splitwise_feb22.services.command.Command;
import org.springframework.stereotype.Service;


public interface CommandRegistry {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ public void executeCommand(String input) {
}
}
}

// serializable

0 comments on commit 19b1894

Please sign in to comment.