Skip to content

Commit

Permalink
Feature: Added associate entity for Doctor
Browse files Browse the repository at this point in the history
  • Loading branch information
elwyncrestha committed Jul 1, 2020
1 parent b75a601 commit 8c63da2
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.pemits.webcare.api.doctor.entity;

import javax.persistence.Entity;
import javax.persistence.OneToOne;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import com.pemits.webcare.api.department.entity.Department;
import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.core.entity.AppUser;
import com.pemits.webcare.core.entity.BaseEntity;
import com.pemits.webcare.core.enums.UserType;

/**
* @author Elvin Shrestha on 7/1/2020
*/
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Doctor extends BaseEntity<Long> implements AppUser {

@OneToOne
private User user;

@OneToOne
private Department department;

private String specializationField;

@Override
public UserType getUserType() {
return UserType.DOCTOR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pemits.webcare.api.doctor.repository;

import org.springframework.stereotype.Repository;

import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.core.repository.BaseRepository;

/**
* @author Elvin Shrestha on 7/1/2020
*/
@Repository
public interface DoctorRepository extends BaseRepository<Doctor, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.pemits.webcare.api.doctor.repository.spec;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.pemits.webcare.api.doctor.entity.Doctor;

/**
* @author Elvin Shrestha on 7/1/2020
*/
public class DoctorSpec implements Specification<Doctor> {

private static final String FILTER_BY_NAME = "user.name";

private final String property;
private final String value;

public DoctorSpec(String property, String value) {
this.property = property;
this.value = value;
}

@Override
public Predicate toPredicate(Root<Doctor> root, CriteriaQuery<?> criteriaQuery,
CriteriaBuilder criteriaBuilder) {
switch (property) {
case FILTER_BY_NAME:
return criteriaBuilder.like(root.join("user").get("name"), "%" + value + "%");
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pemits.webcare.api.doctor.repository.spec;

import java.util.Map;

import org.springframework.data.jpa.domain.Specification;

import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.core.repository.BaseSpecBuilder;

/**
* @author Elvin Shrestha on 7/1/2020
*/
public class DoctorSpecBuilder extends BaseSpecBuilder<Doctor> {

public DoctorSpecBuilder(Map<String, String> params) {
super(params);
}

@Override
protected Specification<Doctor> getSpecification(String property, String filterValue) {
return new DoctorSpec(property, filterValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pemits.webcare.api.doctor.service;

import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.core.service.BaseService;

/**
* @author Elvin Shrestha on 7/1/2020
*/
public interface DoctorService extends BaseService<Doctor, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.pemits.webcare.api.doctor.service;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.api.doctor.repository.DoctorRepository;
import com.pemits.webcare.api.doctor.repository.spec.DoctorSpecBuilder;
import com.pemits.webcare.core.repository.BaseSpecBuilder;
import com.pemits.webcare.core.service.BaseServiceImpl;

/**
* @author Elvin Shrestha on 7/1/2020
*/
@Service
public class DoctorServiceImpl extends BaseServiceImpl<Doctor, Long> implements DoctorService {

private final DoctorRepository repository;

protected DoctorServiceImpl(
DoctorRepository repository) {
super(repository);
this.repository = repository;
}

@Override
protected BaseSpecBuilder<Doctor> getSpec(Map<String, String> filterParams) {
return new DoctorSpecBuilder(filterParams);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pemits.webcare.api.user.service;

import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.core.entity.AppUser;
import com.pemits.webcare.core.service.BaseService;

/**
Expand All @@ -11,4 +12,7 @@ public interface UserService extends BaseService<User, Long> {
User findByUsername(String username);

User getAuthenticated();

AppUser saveAppUser(AppUser appUser);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.api.doctor.service.DoctorService;
import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.api.user.repository.UserRepository;
import com.pemits.webcare.api.user.repository.spec.UserSpecBuilder;
import com.pemits.webcare.core.constant.EmailConstant.Template;
import com.pemits.webcare.core.dto.EmailDto;
import com.pemits.webcare.core.entity.AppUser;
import com.pemits.webcare.core.enums.Status;
import com.pemits.webcare.core.repository.BaseSpecBuilder;
import com.pemits.webcare.core.service.BaseServiceImpl;
Expand All @@ -33,16 +37,21 @@ public class UserServiceImpl extends BaseServiceImpl<User, Long> implements User
private final UserRepository repository;
private final PasswordEncoder passwordEncoder;
private final EmailService emailService;
private final DoctorService doctorService;

protected UserServiceImpl(
UserRepository repository,
PasswordEncoder passwordEncoder, EmailService emailService) {
PasswordEncoder passwordEncoder,
EmailService emailService,
DoctorService doctorService) {
super(repository);
this.repository = repository;
this.passwordEncoder = passwordEncoder;
this.emailService = emailService;
this.doctorService = doctorService;
}

@Transactional
@Override
public User save(User user) {
String password = null;
Expand All @@ -63,8 +72,9 @@ public User save(User user) {

User saved = repository.save(user);

// send email in case of new user
// in case of new user
if (password != null) {
// send email
EmailDto emailDto = EmailDto.builder()
.template(Template.REGISTRATION_CREDENTIALS)
.to(user.getEmail())
Expand All @@ -74,6 +84,15 @@ public User save(User user) {
.webUrl(webUrl)
.build();
emailService.send(emailDto);

// create associate entity
switch (saved.getUserType()) {
case DOCTOR:
Doctor doctor = new Doctor();
doctor.setUser(saved);
saveAppUser(doctor);
break;
}
}

return saved;
Expand Down Expand Up @@ -102,4 +121,18 @@ public User getAuthenticated() {
.getClass() + "; Expected type User");
}
}

@Override
public AppUser saveAppUser(AppUser appUser) {
AppUser savedAppUser;
switch (appUser.getUserType()) {
case DOCTOR:
savedAppUser = doctorService.save((Doctor) appUser);
break;
default:
savedAppUser = null;
}
return savedAppUser;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pemits.webcare.core.entity;

import com.pemits.webcare.core.enums.UserType;

/**
* @author Elvin Shrestha on 7/1/2020
*/
public interface AppUser {

default UserType getUserType() {
return UserType.SUPER_ADMINISTRATOR;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
*/
public enum UserType {
SUPER_ADMINISTRATOR("Super Administrator"),
ADMINISTRATOR("Administrator");
ADMINISTRATOR("Administrator"),
DOCTOR("Doctor"),
PATIENT("Patient"),
NURSE("Nurse"),
RECEPTIONIST("Receptionist"),
STAFF("Staff");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pemits.webcare.web.doctor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.api.doctor.service.DoctorService;
import com.pemits.webcare.core.controller.BaseController;

/**
* @author Elvin Shrestha on 7/1/2020
*/
@RestController
@RequestMapping(DoctorController.URL)
@Slf4j
public class DoctorController extends BaseController<Doctor, Long> {

static final String URL = "/v1/doctors";
private final DoctorService service;

public DoctorController(DoctorService service) {
super(service, log.getClass());
this.service = service;
}
}
34 changes: 34 additions & 0 deletions pemits-web/src/main/resources/db/changelog/changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,38 @@
</addColumn>
</changeSet>

<changeSet id="7-create-doctor" author="Elvin Shrestha">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="doctor"/>
</not>
</preConditions>

<createTable tableName="doctor">
<column name="id" type="BIGINT" autoIncrement="true">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_DOCTOR"/>
</column>
<column name="created_at" type="DATETIME">
<constraints nullable="false"/>
</column>
<column name="last_modified_at" type="DATETIME">
<constraints nullable="false"/>
</column>
<column name="created_by" type="BIGINT"/>
<column name="last_modified_by" type="BIGINT"/>
<column name="version" type="INT"/>
<column name="user_id" type="BIGINT">
<constraints foreignKeyName="FK_DOCTOR_USER_ID_USER_ID"
referencedTableName="users"
referencedColumnNames="id"/>
</column>
<column name="department_id" type="BIGINT">
<constraints foreignKeyName="FK_DOCTOR_DEPARTMENT_ID_DEPARTMENT_ID"
referencedTableName="department"
referencedColumnNames="id"/>
</column>
<column name="specialization_field" type="VARCHAR(255)"/>
</createTable>
</changeSet>

</databaseChangeLog>

0 comments on commit 8c63da2

Please sign in to comment.