Skip to content

Commit

Permalink
Merge pull request #67 from consiglionazionaledellericerche/66-aggior…
Browse files Browse the repository at this point in the history
…namento-e-creazione-dto-per-userinfo-e-personinfo

aggiunti nuovi dto e nuove endpoint per ottenere le informazioni sui turni e reperibilità di una persona e i suoi eventuali ruoli
  • Loading branch information
criluc authored Jan 27, 2025
2 parents 2dcf50e + 5c8695e commit 2734d4a
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 5 deletions.
43 changes: 41 additions & 2 deletions src/main/java/it/cnr/iit/epas/controller/v4/PersonInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
import it.cnr.iit.epas.config.OpenApiConfiguration;
import it.cnr.iit.epas.controller.v4.utils.ApiRoutes;
import it.cnr.iit.epas.dto.v4.PersonShowDto;
import it.cnr.iit.epas.dto.v4.PersonShowExtendedDto;
import it.cnr.iit.epas.dto.v4.mapper.PersonShowExtendedMapper;
import it.cnr.iit.epas.dto.v4.mapper.PersonShowMapper;
import it.cnr.iit.epas.helpers.TemplateUtility;
import it.cnr.iit.epas.models.Person;
import it.cnr.iit.epas.models.User;
import it.cnr.iit.epas.repo.PersonRepository;
import it.cnr.iit.epas.security.SecureUtils;
Expand Down Expand Up @@ -63,17 +67,22 @@ public class PersonInfo {

private PersonRepository repo;
private PersonShowMapper mapper;
private PersonShowExtendedMapper mapperExtend;
private SecureUtils secureUtils;
private TemplateUtility templateUtility;

/**
* Costruttore di default per l'injection.
*/
@Inject
PersonInfo(PersonRepository repo, PersonShowMapper mapper,
SecureUtils securityUtils) {
SecureUtils securityUtils, PersonShowExtendedMapper mapperExtend,
TemplateUtility templateUtility) {
this.repo = repo;
this.mapper = mapper;
this.secureUtils = securityUtils;
this.mapperExtend = mapperExtend;
this.templateUtility = templateUtility;
}

@Operation(
Expand All @@ -95,11 +104,41 @@ ResponseEntity<PersonShowDto> show() {
if (!user.isPresent()) {
return ResponseEntity.badRequest().build();
}
long personId = user.get().getId();
long personId = user.get().getPerson().getId();
val entity = repo.findById(personId)
.orElseThrow(() ->
new EntityNotFoundException("Person not found for user with id = " + user.get().getId()));
return ResponseEntity.ok().body(mapper.convert(entity));
}

@Operation(
summary = "Mostra le informazioni della persona collegata all'utente autenticato.",
description = "Questo endpoint è utilizzabile da tutti gli utenti autenticati.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200",
description = "Restituite le informazioni relative alla persona autenticata."),
@ApiResponse(responseCode = "401",
description = "Autenticazione non presente", content = @Content),
@ApiResponse(responseCode = "404",
description = "Utente che ha effettuato la richiesta non ha associato nessuna persona.",
content = @Content)
})
@GetMapping("/extend")
ResponseEntity<PersonShowExtendedDto> extend() {
Optional<User> user = secureUtils.getCurrentUser();
log.debug("UserInfo::show user = {}", user.orElse(null));
if (!user.isPresent()) {
return ResponseEntity.badRequest().build();
}
Person person = user.get().getPerson();
boolean isAvailable = templateUtility.isAvailable(person);
long personId = person.getId();
val entity = repo.findById(personId)
.orElseThrow(() ->
new EntityNotFoundException("Person not found for user with id = " + user.get().getId()));
PersonShowExtendedDto dto = mapperExtend.convert(entity);
dto.setAvailable(isAvailable);
return ResponseEntity.ok().body(dto);
}

}
17 changes: 15 additions & 2 deletions src/main/java/it/cnr/iit/epas/controller/v4/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package it.cnr.iit.epas.controller.v4;

import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -29,8 +30,11 @@
import it.cnr.iit.epas.dto.v4.UserShowDto;
import it.cnr.iit.epas.dto.v4.mapper.UserShowMapper;
import it.cnr.iit.epas.models.User;
import it.cnr.iit.epas.models.UsersRolesOffices;
import it.cnr.iit.epas.models.absences.CategoryTab;
import it.cnr.iit.epas.repo.UserRepository;
import it.cnr.iit.epas.security.SecureUtils;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import javax.persistence.EntityNotFoundException;
Expand Down Expand Up @@ -96,8 +100,17 @@ ResponseEntity<UserShowDto> show() {
return ResponseEntity.badRequest().build();
}
long userId = user.get().getId();
val entity = repo.findById(userId)
User entity = repo.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User not found"));
return ResponseEntity.ok().body(mapper.convert(entity));

List<String> rolesOffice = Lists.newArrayList();
for (UsersRolesOffices role : entity.getUsersRolesOffices()) {
rolesOffice.add(role.role.toString());
}
UserShowDto dto = mapper.convert(entity);
dto.setRolesOffice(rolesOffice);
log.debug("UserInfo::show roles = {}", entity.getUsersRolesOffices());

return ResponseEntity.ok().body(dto);
}
}
36 changes: 36 additions & 0 deletions src/main/java/it/cnr/iit/epas/dto/v4/PersonShiftDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2022 Consiglio Nazionale delle Ricerche
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package it.cnr.iit.epas.dto.v4;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* DTO per l'esportazione via REST delle informazioni
* principali di un'assenza.
*
* @since versione 4 dell'API REST
* @author Cristian Lucchesi
*
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class PersonShiftDto extends BaseModelDto {

private String description;
}
5 changes: 4 additions & 1 deletion src/main/java/it/cnr/iit/epas/dto/v4/PersonShowDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

package it.cnr.iit.epas.dto.v4;

import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.media.Schema;
import it.cnr.iit.epas.models.PersonReperibilityType;
import it.cnr.iit.epas.models.ShiftCategories;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand Down Expand Up @@ -51,5 +55,4 @@ public class PersonShowDto extends PersonMutableDto {

@Schema(description = "Residenza")
private String residence;

}
44 changes: 44 additions & 0 deletions src/main/java/it/cnr/iit/epas/dto/v4/PersonShowExtendedDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2023 Consiglio Nazionale delle Ricerche
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package it.cnr.iit.epas.dto.v4;

import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.media.Schema;
import it.cnr.iit.epas.models.PersonReperibilityType;
import it.cnr.iit.epas.models.PersonShift;
import it.cnr.iit.epas.models.ShiftCategories;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* DTO per mostrare i dati estesi di una persona.
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class PersonShowExtendedDto extends PersonShowDto {

private boolean available;
private List<ShiftCategoryDto> shiftCategories = Lists.newArrayList();
private List<PersonReperibilityTypeDto> reperibilityTypes = Lists.newArrayList();
private List<ShiftCategoryDto> categories = Lists.newArrayList();
private List<PersonReperibilityTypeDto> reperibilities = Lists.newArrayList();
private List<PersonShiftDto> personShifts = Lists.newArrayList();
}
30 changes: 30 additions & 0 deletions src/main/java/it/cnr/iit/epas/dto/v4/ShiftCategoryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2022 Consiglio Nazionale delle Ricerche
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package it.cnr.iit.epas.dto.v4;

import java.time.YearMonth;
import lombok.Data;

/**
* DTO per ShiftCategory.
*/
@Data
public class ShiftCategoryDto {

private String description;
}
5 changes: 5 additions & 0 deletions src/main/java/it/cnr/iit/epas/dto/v4/UserShowDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package it.cnr.iit.epas.dto.v4;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.swagger.v3.oas.annotations.media.Schema;
import it.cnr.iit.epas.models.enumerate.AccountRole;
import java.util.List;
import java.util.Set;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -40,4 +42,7 @@ public class UserShowDto extends UserShowTerseDto {
@Schema(description = "abilitato si/no")
private boolean disabled;

@Schema(description = "Ruoli di ufficio attribuiti all'utente")
private List<String> rolesOffice = Lists.newArrayList();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2023 Consiglio Nazionale delle Ricerche
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package it.cnr.iit.epas.dto.v4.mapper;

import it.cnr.iit.epas.dto.v4.OfficeShowTerseDto;
import it.cnr.iit.epas.dto.v4.PersonReperibilityTypeDto;
import it.cnr.iit.epas.dto.v4.PersonReperibilityTypeTerseDto;
import it.cnr.iit.epas.dto.v4.PersonShiftDto;
import it.cnr.iit.epas.dto.v4.PersonShowDto;
import it.cnr.iit.epas.dto.v4.PersonShowExtendedDto;
import it.cnr.iit.epas.dto.v4.ShiftCategoryDto;
import it.cnr.iit.epas.dto.v4.UserShowTerseDto;
import it.cnr.iit.epas.models.Office;
import it.cnr.iit.epas.models.Person;
import it.cnr.iit.epas.models.PersonReperibilityType;
import it.cnr.iit.epas.models.PersonShift;
import it.cnr.iit.epas.models.ShiftCategories;
import it.cnr.iit.epas.models.User;
import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

/**
* Mapper da Person al suo DTO per la visualizzazione via REST.
*/
@Mapper(componentModel = "spring")
public interface PersonShowExtendedMapper {
List<ShiftCategoryDto> convertShiftCategories(List<ShiftCategories> shiftCategories);
List<PersonReperibilityTypeDto> convertPersonReperibility(List<PersonReperibilityType> reperibilities);
List<PersonShiftDto> convertPersonShift(List<PersonShift> personShift);

PersonReperibilityTypeTerseDto convert(PersonReperibilityType reperibility);


@Mapping(target = "birthDate", source = "birthday")
@Mapping(target = "qualification", source = "qualification.id")
PersonShowExtendedDto convert(Person person);

OfficeShowTerseDto convert(Office office);

UserShowTerseDto convert(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@
import it.cnr.iit.epas.dto.v4.UserShowDto;
import it.cnr.iit.epas.dto.v4.UserShowTerseDto;
import it.cnr.iit.epas.models.User;
import it.cnr.iit.epas.models.absences.JustifiedType;
import it.cnr.iit.epas.models.enumerate.AccountRole;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ValueMapping;

/**
* Mapper per User al corrispondente DTO per la visualizzazione via REST.
*/
@Mapper(componentModel = "spring")
public interface UserShowMapper {

//@ValueMapping(target = ".", source = "name")
//String convert(AccountRole account);

@Mapping(target = "personId", source = "person.id")
@Mapping(target = "ownerId", source = "owner.id")
UserShowDto convert(User user);
Expand Down

0 comments on commit 2734d4a

Please sign in to comment.