Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<version>${jsonwebtoken.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.20.26</version>
</dependency>

</dependencies>

Expand Down
56 changes: 39 additions & 17 deletions backend/src/main/java/com/amigoscode/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.amigoscode.customer.Customer;
import com.amigoscode.customer.CustomerRepository;
import com.amigoscode.customer.Gender;
import com.amigoscode.s3.S3Buckets;
import com.amigoscode.s3.S3Service;
import com.github.javafaker.Faker;
import com.github.javafaker.Name;
import org.springframework.boot.CommandLineRunner;
Expand All @@ -24,25 +26,45 @@ public static void main(String[] args) {
@Bean
CommandLineRunner runner(
CustomerRepository customerRepository,
PasswordEncoder passwordEncoder) {
PasswordEncoder passwordEncoder)
{
return args -> {
var faker = new Faker();
Random random = new Random();
Name name = faker.name();
String firstName = name.firstName();
String lastName = name.lastName();
int age = random.nextInt(16, 99);
Gender gender = age % 2 == 0 ? Gender.MALE : Gender.FEMALE;
String email = firstName.toLowerCase() + "." + lastName.toLowerCase() + "@amigoscode.com";
Customer customer = new Customer(
firstName + " " + lastName,
email,
passwordEncoder.encode("password"),
age,
gender);
customerRepository.save(customer);
System.out.println(email);
createRandomCustomer(customerRepository, passwordEncoder);
//testBucketUploadandDownload(s3Service, s3Buckets);
};
}

private static void testBucketUploadandDownload(S3Service s3Service, S3Buckets s3Buckets) {
s3Service.putObject(
s3Buckets.getCustomer(),
"foo",
"Hello World".getBytes());

byte[] obj = s3Service.getObject(
s3Buckets.getCustomer(),
"foo"
);

System.out.println("Hooray: " + new String(obj));
}

private static void createRandomCustomer(CustomerRepository customerRepository, PasswordEncoder passwordEncoder) {
var faker = new Faker();
Random random = new Random();
Name name = faker.name();
String firstName = name.firstName();
String lastName = name.lastName();
int age = random.nextInt(16, 99);
Gender gender = age % 2 == 0 ? Gender.MALE : Gender.FEMALE;
String email = firstName.toLowerCase() + "." + lastName.toLowerCase() + "@amigoscode.com";
Customer customer = new Customer(
firstName + " " + lastName,
email,
passwordEncoder.encode("password"),
age,
gender);
customerRepository.save(customer);
System.out.println(email);
}

}
73 changes: 53 additions & 20 deletions backend/src/main/java/com/amigoscode/customer/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import software.amazon.awssdk.services.s3.endpoints.internal.Value;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -17,6 +18,10 @@
@UniqueConstraint(
name = "customer_email_unique",
columnNames = "email"
),
@UniqueConstraint(
name = "profile_image_id_unique",
columnNames = "profileImageId"
)
}
)
Expand Down Expand Up @@ -57,6 +62,11 @@ public class Customer implements UserDetails {
)
private String password;

@Column(
unique = true
)
private String profileImageId;

public Customer() {
}

Expand Down Expand Up @@ -86,6 +96,17 @@ public Customer(String name,
this.gender = gender;
}

public Customer(Integer id,
String name,
String email,
String password,
Integer age,
Gender gender,
String profileImageId) {
this(id, name, email, password, age, gender);
this.profileImageId = profileImageId;
}

public Integer getId() {
return id;
}
Expand Down Expand Up @@ -126,28 +147,12 @@ public void setGender(Gender gender) {
this.gender = gender;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, customer.id) && Objects.equals(name, customer.name) && Objects.equals(email, customer.email) && Objects.equals(age, customer.age) && gender == customer.gender;
public String getProfileImageId(){
return profileImageId;
}

@Override
public int hashCode() {
return Objects.hash(id, name, email, age, gender);
}

@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
", gender=" + gender +
'}';
public void setProfileImageId(String profileImageId) {
this.profileImageId = profileImageId;
}

@Override
Expand Down Expand Up @@ -184,4 +189,32 @@ public boolean isCredentialsNonExpired() {
public boolean isEnabled() {
return true;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, customer.id) && Objects.equals(name, customer.name) && Objects.equals(email, customer.email) && Objects.equals(age, customer.age) && gender == customer.gender && Objects.equals(password, customer.password) && Objects.equals(profileImageId, customer.profileImageId);
}

@Override
public int hashCode() {
return Objects.hash(id, name, email, age, gender, password, profileImageId);
}

@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
", gender=" + gender +
", password='" + password + '\'' +
", profileImageId='" + profileImageId + '\'' +
'}';
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.amigoscode.jwt.JWTUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
Expand Down Expand Up @@ -54,4 +57,20 @@ public void updateCustomer(
customerService.updateCustomer(customerId, updateRequest);
}

@PostMapping(
value = "{customerId}/profile-image",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

public void uploadCustomerProfileImage(
@PathVariable("customerId") Integer customerID,
@RequestParam("file") MultipartFile file) throws IOException {
customerService.uploadCustomerImage(customerID, file);
}


@GetMapping("{customerId}/profile-image")
public byte[] getCustomerProfileImage(
@PathVariable("customerId") Integer customerId) {
return customerService.getCustomerProfileImage(customerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record CustomerDTO (
Gender gender,
Integer age,
List<String> roles,
String username
){
String username,
String profileImageId){

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public CustomerDTO apply(Customer customer) {
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList()),
customer.getUsername()
customer.getUsername(),
customer.getProfileImageId()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

public interface CustomerDao {
List<Customer> selectAllCustomers();
Optional<Customer> selectCustomerById(Integer id);
Optional<Customer> selectCustomerById(Integer customerId);
void insertCustomer(Customer customer);
boolean existsCustomerWithEmail(String email);
boolean existsCustomerById(Integer id);
boolean existsCustomerById(Integer customerId);
void deleteCustomerById(Integer customerId);
void updateCustomer(Customer update);
Optional<Customer> selectUserByEmail(String email);
void updateCustomerProfileImageId(String profileImageId, Integer customerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CustomerJDBCDataAccessService(JdbcTemplate jdbcTemplate,
@Override
public List<Customer> selectAllCustomers() {
var sql = """
SELECT id, name, email, password, age, gender
SELECT id, name, email, password, age, gender, profile_image_id
FROM customer
LIMIT 1000
""";
Expand All @@ -32,7 +32,7 @@ public List<Customer> selectAllCustomers() {
@Override
public Optional<Customer> selectCustomerById(Integer id) {
var sql = """
SELECT id, name, email, password, age, gender
SELECT id, name, email, password, age, gender, profile_image_id
FROM customer
WHERE id = ?
""";
Expand Down Expand Up @@ -125,12 +125,23 @@ public void updateCustomer(Customer update) {
@Override
public Optional<Customer> selectUserByEmail(String email) {
var sql = """
SELECT id, name, email, password, age, gender
SELECT id, name, email, password, age, gender, profile_image_id
FROM customer
WHERE email = ?
""";
return jdbcTemplate.query(sql, customerRowMapper, email)
.stream()
.findFirst();
}

@Override
public void updateCustomerProfileImageId(String profileImageId, Integer customerId) {
var sql = """
UPDATE customer
SET profile_image_id = ?
WHERE id= ?
""";
jdbcTemplate.update(sql, profileImageId, customerId);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ public Optional<Customer> selectUserByEmail(String email) {
return customerRepository.findCustomerByEmail(email);
}

@Override
public void updateCustomerProfileImageId(String profileImageId, Integer customerId) {
customerRepository.updateProfileImageId(profileImageId, customerId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public Optional<Customer> selectUserByEmail(String email) {
.findFirst();
}

@Override
public void updateCustomerProfileImageId(String profileImageId, Integer customerId) {

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.amigoscode.customer;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

Expand All @@ -10,4 +12,9 @@ public interface CustomerRepository
boolean existsCustomerByEmail(String email);
boolean existsCustomerById(Integer id);
Optional<Customer> findCustomerByEmail(String email);

@Modifying
@Query("UPDATE Customer c SET c.profileImageId = ?1 WHERE c.id = ?2")
int updateProfileImageId(String profileImageId, Integer customerId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
rs.getString("email"),
rs.getString("password"),
rs.getInt("age"),
Gender.valueOf(rs.getString("gender")));
Gender.valueOf(rs.getString("gender")),
rs.getString(("profile_image_id")));
}
}
Loading