Skip to content

Commit

Permalink
Feature: Added user type and API to update the user status.
Browse files Browse the repository at this point in the history
  • Loading branch information
elwyncrestha committed Jun 26, 2020
1 parent 2368147 commit 568d6e5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import com.pemits.webcare.core.entity.BaseEntity;
import com.pemits.webcare.core.enums.Status;
import com.pemits.webcare.core.enums.UserType;

/**
* @author Elvin Shrestha on 6/21/2020
Expand All @@ -30,6 +31,7 @@ public class User extends BaseEntity<Long> implements UserDetails {
private String name;
private String email;
private Status status;
private UserType userType;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import org.springframework.data.jpa.domain.Specification;

import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.core.enums.UserType;

/**
* @author Elvin Shrestha on 6/21/2020
*/
public class UserSpec implements Specification<User> {

private static final String FILTER_BY_NAME = "name";
private static final String FILTER_BY_USER_TYPE = "userType";

private final String property;
private final String value;

Expand All @@ -25,6 +29,13 @@ public UserSpec(String property, String value) {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery,
CriteriaBuilder criteriaBuilder) {
return null;
switch (property) {
case FILTER_BY_NAME:
return criteriaBuilder.like(root.get(FILTER_BY_NAME), "%" + value + "%");
case FILTER_BY_USER_TYPE:
return criteriaBuilder.equal(root.get(FILTER_BY_USER_TYPE), UserType.valueOf(value));
default:
return null;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.pemits.webcare.core.enums;

/**
* @author Elvin Shrestha on 6/21/2020
* @author Elvin Shrestha on 6/26/2020
*/
public enum RoleAccess {

OWN("Own"), SPECIFIC("Specific"), ALL("All");
public enum UserType {
SUPER_ADMINISTRATOR("Super Administrator"),
ADMINISTRATOR("Administrator");

private final String value;

RoleAccess(String value) {
UserType(String value) {
this.value = value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.pemits.webcare.web.user;

import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.api.user.service.UserService;
import com.pemits.webcare.core.controller.BaseController;
import com.pemits.webcare.core.dto.RestResponseDto;
import com.pemits.webcare.core.enums.Status;

/**
* @author Elvin Shrestha on 6/21/2020
Expand All @@ -33,4 +38,15 @@ protected UserController(
public ResponseEntity<?> getAuthenticated() {
return new RestResponseDto().success(service.getAuthenticated());
}

@GetMapping("/changeStatus")
public ResponseEntity<?> changeStatus(@RequestParam Long id, @RequestParam Status status) {
User user = service.findOne(id).orElse(null);
if (null == user) {
return new RestResponseDto().fail(HttpStatus.NOT_FOUND, Optional.empty());
}

user.setStatus(status);
return new RestResponseDto().success(service.save(user));
}
}
56 changes: 34 additions & 22 deletions pemits-web/src/main/resources/db/changelog/changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,41 @@
</createTable>
</changeSet>

<changeSet id="3-create-department" author="Mohammad Hussain">
<changeSet id="3-create-department" author="Mohammad Hussain">

<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="department"/>
</not>
</preConditions>
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="department"/>
</not>
</preConditions>

<createTable tableName="department">
<column name="id" type="BIGINT" autoIncrement="true">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_DEPARTMENT"/>
</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="name" type="VARCHAR(255)"/>
</createTable>
</changeSet>

<createTable tableName="department">
<column name="id" type="BIGINT" autoIncrement="true">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_DEPARTMENT"/>
</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="name" type="VARCHAR(255)"/>
</createTable>
</changeSet>
<changeSet id="4-alter-users-add-user_type" author="Elvin Shrestha">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="users" columnName="user_type"/>
</not>
</preConditions>

<addColumn tableName="users">
<column name="user_type" type="INT"/>
</addColumn>
</changeSet>

</databaseChangeLog>
4 changes: 2 additions & 2 deletions pemits-web/src/main/resources/db/patch/initial_user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ BEGIN
BEGIN
SET IDENTITY_INSERT users ON
INSERT INTO users(id, created_at, last_modified_at, created_by, last_modified_by,
version, username, password, name, email, status)
version, username, password, name, email, status, user_type)
VALUES (1, '2020-06-20 15:30:00', '2020-06-20 15:30:00', NULL, NULL, 0, 'spadmin',
'$2a$10$CGkAwfRBRIMVoEX8Ui9yx.NC03wKCjE19KIGVNET2F1mn0o58jkly',
'The Administrator', NULL, 1)
'The Administrator', NULL, 1, 0)
SET IDENTITY_INSERT users OFF
END
END;

0 comments on commit 568d6e5

Please sign in to comment.