generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 11
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
11 changed files
with
401 additions
and
27 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
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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/softserveinc/dokazovi/controller/LogForLoginController.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,57 @@ | ||
package com.softserveinc.dokazovi.controller; | ||
|
||
import com.softserveinc.dokazovi.annotations.ApiPageable; | ||
import com.softserveinc.dokazovi.entity.LogForLoginEntity; | ||
import com.softserveinc.dokazovi.service.LogForLoginService; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiParam; | ||
import io.swagger.annotations.Authorization; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static com.softserveinc.dokazovi.controller.EndPoints.LOG_FOR_LOGIN; | ||
|
||
@RestController | ||
@RequestMapping(LOG_FOR_LOGIN) | ||
@RequiredArgsConstructor | ||
public class LogForLoginController { | ||
private final LogForLoginService logForLoginService; | ||
|
||
@GetMapping() | ||
@PreAuthorize("hasAuthority('EDIT_AUTHOR')") | ||
@ApiPageable | ||
@ApiOperation(value = "get all logs", | ||
authorizations = {@Authorization(value = "Authorization")}) | ||
public ResponseEntity<Page<LogForLoginEntity>> getLogList( | ||
@PageableDefault(size = 24, direction = Sort.Direction.DESC) Pageable pageable, | ||
@ApiParam(value = "yyyy-MM-dd") | ||
@RequestParam(required = false) | ||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate, | ||
@ApiParam(value = "yyyy-MM-dd") | ||
@RequestParam(required = false) | ||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate, | ||
@ApiParam(value = "Logs by status", type = "string") | ||
@RequestParam(required = false, defaultValue = "") String status, | ||
@ApiParam(value = "Logs by login", type = "string") | ||
@RequestParam(required = false, defaultValue = "") String login, | ||
@ApiParam(value = "Logs by ip", type = "string") | ||
@RequestParam(required = false, defaultValue = "") String ip | ||
) { | ||
return ResponseEntity | ||
.status(HttpStatus.OK) | ||
.body(logForLoginService.findAllLogs(pageable, startDate, endDate, status, login, ip)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/softserveinc/dokazovi/entity/LogForLoginEntity.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,43 @@ | ||
package com.softserveinc.dokazovi.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import javax.validation.constraints.NotBlank; | ||
import java.sql.Timestamp; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Entity(name = "log_for_login_entity") | ||
@Table(name = "log_for_login") | ||
public class LogForLoginEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "log_id") | ||
private Integer id; | ||
|
||
@NotBlank | ||
private String login; | ||
|
||
@CreationTimestamp | ||
private Timestamp dateOfLogin; | ||
|
||
@NotBlank | ||
private String ip; | ||
|
||
@NotBlank | ||
private String loginStatus; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/softserveinc/dokazovi/repositories/LogForLoginRepository.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,20 @@ | ||
package com.softserveinc.dokazovi.repositories; | ||
|
||
import com.softserveinc.dokazovi.entity.LogForLoginEntity; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.sql.Timestamp; | ||
|
||
public interface LogForLoginRepository extends JpaRepository<LogForLoginEntity, Integer> { | ||
void deleteLogForLoginEntitiesByDateOfLoginBefore(Timestamp minDate); | ||
|
||
Page<LogForLoginEntity> findByDateOfLoginBetween(Pageable pageable, Timestamp start, Timestamp end); | ||
|
||
Page<LogForLoginEntity> findByLoginStatusContainingIgnoreCase(Pageable pageable, String status); | ||
|
||
Page<LogForLoginEntity> findByLoginContainingIgnoreCase(Pageable pageable, String login); | ||
|
||
Page<LogForLoginEntity> findAllByIpContaining(Pageable pageable, String ip); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/softserveinc/dokazovi/service/LogForLoginService.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,16 @@ | ||
package com.softserveinc.dokazovi.service; | ||
|
||
import com.softserveinc.dokazovi.entity.LogForLoginEntity; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.time.LocalDate; | ||
|
||
public interface LogForLoginService { | ||
LogForLoginEntity save(LogForLoginEntity log); | ||
|
||
Page<LogForLoginEntity> findAllLogs(Pageable pageable, LocalDate start, LocalDate end, | ||
String status, String login, String ip); | ||
|
||
void deleteOutdatedLogs(); | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/softserveinc/dokazovi/service/impl/LogForLoginServiceImpl.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,61 @@ | ||
package com.softserveinc.dokazovi.service.impl; | ||
|
||
import com.softserveinc.dokazovi.entity.LogForLoginEntity; | ||
import com.softserveinc.dokazovi.repositories.LogForLoginRepository; | ||
import com.softserveinc.dokazovi.service.LogForLoginService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LogForLoginServiceImpl implements LogForLoginService { | ||
|
||
private final LogForLoginRepository logForLoginRepository; | ||
|
||
@Override | ||
public LogForLoginEntity save(LogForLoginEntity log) { | ||
return logForLoginRepository.save(log); | ||
} | ||
|
||
@Override | ||
public Page<LogForLoginEntity> findAllLogs(Pageable pageable, LocalDate startDate, LocalDate endDate, | ||
String status, String login, String ip) { | ||
if (startDate != null || endDate != null) { | ||
LocalDate startLocalDate = Optional.ofNullable(startDate).orElse(LocalDate.EPOCH); | ||
LocalDate endLocalDate = Optional.ofNullable(endDate).orElse(LocalDate.now()); | ||
Timestamp startDateTimestamp = Timestamp.valueOf(startLocalDate.atStartOfDay()); | ||
Timestamp endDateTimestamp = Timestamp.valueOf(endLocalDate.atTime(LocalTime.MAX)); | ||
return logForLoginRepository.findByDateOfLoginBetween(pageable, startDateTimestamp, endDateTimestamp); | ||
} | ||
if (status != null && status.trim().length() > 0) { | ||
return logForLoginRepository.findByLoginStatusContainingIgnoreCase(pageable, status); | ||
} | ||
if (login != null && login.trim().length() > 0) { | ||
return logForLoginRepository.findByLoginContainingIgnoreCase(pageable, login); | ||
} | ||
if (ip != null && ip.trim().length() > 0) { | ||
return logForLoginRepository.findAllByIpContaining(pageable, ip); | ||
} | ||
return logForLoginRepository.findAll(pageable); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
@Scheduled(cron = "0 0 12 * * ?") | ||
public void deleteOutdatedLogs() { | ||
logForLoginRepository.deleteLogForLoginEntitiesByDateOfLoginBefore( | ||
Timestamp.valueOf(LocalDateTime.now().minusMonths(3)) | ||
); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/resources/db/migration/V33__add_log_for_login_table.sql
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,8 @@ | ||
CREATE TABLE LOG_FOR_LOGIN | ||
( | ||
LOG_ID serial not null primary key, | ||
LOGIN VARCHAR, | ||
DATE_OF_LOGIN TIMESTAMP DEFAULT NOW(), | ||
IP VARCHAR, | ||
LOGIN_STATUS VARCHAR | ||
); |
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
Oops, something went wrong.