Skip to content

Commit

Permalink
bug fixed concerning unicity codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jesussmariscal committed Dec 28, 2024
1 parent c8668bd commit bd9ef7c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
24 changes: 2 additions & 22 deletions Backend/src/main/java/com/example/model/Attendance.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.springframework.beans.factory.annotation.Autowired;

import com.example.services.AttendanceService;
import com.fasterxml.jackson.annotation.JsonIgnore;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -12,11 +11,9 @@
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;

import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import jakarta.persistence.OneToMany;

Expand All @@ -40,20 +37,11 @@ public class Attendance {
@OneToMany(mappedBy = "attendance")
private List<UserAttendance> usersPresent = new ArrayList<UserAttendance>();

private static final int CODE_LENGTH = 6;
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final Random random = new SecureRandom();

@Autowired
private AttendanceService attendanceService;

public Attendance(User user, Subject subject) {
public Attendance(User user, Subject subject, String code) {
this.creator = user;
this.subject = subject;
dateTime = LocalDateTime.now();
do {
this.code = generateRandomCode(CODE_LENGTH);
} while (attendanceService.isCodeUsed(code));
this.code = code;

}

Expand Down Expand Up @@ -84,14 +72,6 @@ public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}

private String generateRandomCode(int length) {
StringBuilder code = new StringBuilder(length);
for (int i = 0; i < length; i++) {
code.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
}
return code.toString();
}

public void moreTime() {
dateTime = LocalDateTime.now();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.services;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -21,8 +23,12 @@ public class AttendanceService {
@Autowired
private UserAttendanceRepository userAttendanceRepository;

private static final int CODE_LENGTH = 6;
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final Random random = new SecureRandom();

public Attendance newAttendance(User user, Subject subject) {
Attendance attendance = new Attendance(user, subject);
Attendance attendance = new Attendance(user, subject, generateRandomCode(CODE_LENGTH));
attendanceRepository.save(attendance);
return attendance;

Expand Down Expand Up @@ -57,4 +63,17 @@ public Attendance getAttendanceById(Long id) {
return attendanceRepository.findById(id).get();
}

private String generateRandomCode(int length) {
String code;
do {
StringBuilder codeBuilder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
codeBuilder.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
}
code = codeBuilder.toString();
} while (attendanceRepository.existsByCode(code));
return code;
}


}
Binary file modified Backend/target/classes/com/example/model/Attendance.class
Binary file not shown.
Binary file modified Backend/target/classes/com/example/services/AttendanceService.class
Binary file not shown.
Binary file not shown.

0 comments on commit bd9ef7c

Please sign in to comment.