Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
feat: Airline Register
Browse files Browse the repository at this point in the history
  • Loading branch information
SakuraPuare committed May 31, 2024
1 parent 246ce26 commit 22592fc
Show file tree
Hide file tree
Showing 31 changed files with 382 additions and 135 deletions.
18 changes: 16 additions & 2 deletions sql/flightmanagement.session.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
drop database flightmanagement;
create database flightmanagement;
USE `flightmanagement`;
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(11) NULL AUTO_INCREMENT,
`user_id` BIGINT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NULL,
Expand All @@ -24,4 +26,16 @@ VALUES (
'admin',
'[email protected]',
15
);
);
CREATE TABLE IF NOT EXISTS `airline` (
`airline_id` BIGINT NULL AUTO_INCREMENT,
`user_id` BIGINT NOT NULL,
`airline_name` varchar(100) NOT NULL,
`airline_country` varchar(100) NOT NULL,
`airline_code` varchar(10) NOT NULL,
`airline_description` varchar(255) NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`airline_id`),
FOREIGN KEY (`user_id`) REFERENCES `user`(`user_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.sakurapuare.flightmanagement.controllers;

import com.sakurapuare.flightmanagement.common.Response;
import com.sakurapuare.flightmanagement.mapper.UserMapper;
import com.sakurapuare.flightmanagement.constant.UserType;
import com.sakurapuare.flightmanagement.mapper.*;
import com.sakurapuare.flightmanagement.pojo.dto.auth.login.UserLoginDTO;
import com.sakurapuare.flightmanagement.pojo.dto.auth.register.BaseUserRegisterDTO;
import com.sakurapuare.flightmanagement.pojo.dto.auth.register.UserRegisterDTO;
import com.sakurapuare.flightmanagement.pojo.entity.User;
import com.sakurapuare.flightmanagement.pojo.entity.user.Airline;
import com.sakurapuare.flightmanagement.pojo.entity.user.User;
import com.sakurapuare.flightmanagement.pojo.entity.user.info.AirlineInfo;
import com.sakurapuare.flightmanagement.pojo.vo.UserLoginVO;
import com.sakurapuare.flightmanagement.services.AuthService;
import com.sakurapuare.flightmanagement.utils.UserTypeUtils;
Expand All @@ -13,6 +17,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -30,8 +35,20 @@ public class AuthController {
@Autowired
private UserMapper userMapper;

@Autowired
private AirlineMapper airlineMapper;

@Autowired
private MerchantMapper merchantMapper;

@Autowired
private PassengerMapper passengerMapper;

@Autowired
private StaffMapper staffMapper;

@PostMapping("/login")
public Response<UserLoginVO> login(@Valid @RequestBody UserLoginDTO userLoginDTO) {
public Response<UserLoginVO> login(@Validated @RequestBody UserLoginDTO userLoginDTO) {
User user = userMapper.findUserByUsernameAndPassword(
userLoginDTO.getUsername(),
userLoginDTO.getPassword());
Expand All @@ -44,28 +61,107 @@ public Response<UserLoginVO> login(@Valid @RequestBody UserLoginDTO userLoginDTO
}
}

@PostMapping("/register")
public Response<Void> register(@RequestBody UserRegisterDTO userRegisterDTO) {
// LOG
log.info("Register: {}", userRegisterDTO);
@PostMapping("/register/airline")
public Response<Void> register_airline(
@Valid @RequestBody UserRegisterDTO<AirlineInfo> airlineInfoUserRegisterDTO) {
User user = userMapper.findUserByUsername(airlineInfoUserRegisterDTO.getUsername());
Airline airline = airlineMapper.findAirlineByAirlineCode(airlineInfoUserRegisterDTO.getData().getAirlineCode());

if (user != null && airline != null) {
return Response.error("Airline already exists");
}

// Register Role
User user = userMapper.findUserByUsername(userRegisterDTO.getUsername());
int role = userRegisterDTO.getRole();
// check password
if (user != null) {
if (!user.getPassword().equals(userRegisterDTO.getPassword()))
return Response.error("Invalid password");
if (UserTypeUtils.isContain(user.getRole(), role))
return Response.error("User already exists");
user.setRole(UserTypeUtils.addRole(user.getRole(), role));
if (!airlineInfoUserRegisterDTO.getPassword().equals(user.getPassword())) {
return Response.error("Password not match");
}
}

if (user == null) {
user = new User();
BeanUtils.copyProperties(airlineInfoUserRegisterDTO, user);
user.setRole(UserType.AIRLINE);
userMapper.insert(user);
}
if (airline == null) {
airline = new Airline();
BeanUtils.copyProperties(airlineInfoUserRegisterDTO.getData(), airline);
user.setRole(UserTypeUtils.addRole(user.getRole(), UserType.AIRLINE));
airline.setUserId(user.getUserId());
airlineMapper.insert(airline);
userMapper.updateById(user);
return Response.success("Update role success");
} else {
return Response.success("Airline Register success");
}
return Response.error("User already exists");
}

// @PostMapping("/register/merchant")
// public Response<Void> register_merchant(@Valid @RequestBody
// MerchantRegisterDTO merchantRegisterDTO) {
// User user = userMapper.findUserByUsername(merchantRegisterDTO.getUsername());
// Merchant merchant =
// merchantMapper.findMerchantByMerchantCode(merchantRegisterDTO.getData().getMerchantCode());

// if (user == null) {
// user = new User();
// BeanUtils.copyProperties(merchantRegisterDTO, user);
// user.setRole(UserType.MERCHANT);
// userMapper.insert(user);
// return Response.success("Merchant Register success");
// }
// return Response.error("User already exists");
// }

@PostMapping("/register")
public Response<Void> register_user(@Valid @RequestBody BaseUserRegisterDTO baseUserRegisterDTO) {
User user = userMapper.findUserByUsername(baseUserRegisterDTO.getUsername());
if (user == null) {
user = new User();
BeanUtils.copyProperties(userRegisterDTO, user);
BeanUtils.copyProperties(baseUserRegisterDTO, user);
userMapper.insert(user);
return Response.success("Register success");
}
return Response.success("Register success");
return Response.error("User already exists");
}

// public Response<Void> register_user(@Valid @RequestBody UserRegisterDTO
// userRegisterDTO) {
// User user = userMapper.findUserByUsername(userRegisterDTO.getUsername());
// int role = userRegisterDTO.getRole();

// // ALL type is not allowed to register
// if (role == UserType.ALL || UserTypeUtils.getRoleCount(role) != 1)
// return Response.error("Invalid role");
// if (userRegisterDTO.getData() == null)
// return Response.error("User info is required");

// if (user == null) {
// user = new User();
// BeanUtils.copyProperties(userRegisterDTO, user);
// userMapper.insert(user);
// if (role != 0) {
// if (userRegisterDTO.getData() == null)
// return Response.error("User info is required");

// UserInfo userInfo = UserRegisterInfoFactory.create(userRegisterDTO, role);
// if (userInfo == null || !userInfo.checkDataIntegrity())
// return Response.error("Data integrity error");
// }
// } else {
// if (!user.getPassword().equals(userRegisterDTO.getPassword()))
// return Response.error("Invalid password");
// if (UserTypeUtils.isContain(user.getRole(), role))
// return Response.error("User already exists");
// if (userRegisterDTO.getData() == null)
// return Response.error("User info is required");

// UserInfo userInfo = UserRegisterInfoFactory.create(userRegisterDTO, role);
// if (userInfo == null || !userInfo.checkDataIntegrity())
// return Response.error("Data integrity error");
// }

// return Response.success("Register success");
// }

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sakurapuare.flightmanagement.controllers;


import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -12,5 +11,4 @@
@Tag(name = "User Controller", description = "User API Endpoints")
public class UserController {


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.sakurapuare.flightmanagement.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sakurapuare.flightmanagement.pojo.entity.Airline;
import com.sakurapuare.flightmanagement.pojo.entity.user.Airline;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AirlineMapper extends BaseMapper<Airline> {

default Airline findAirlineByAirlineCode(String airlineCode) {
return this.selectOne(
new QueryWrapper<Airline>()
.eq("airline_code", airlineCode));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sakurapuare.flightmanagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sakurapuare.flightmanagement.pojo.entity.Merchant;
import com.sakurapuare.flightmanagement.pojo.entity.user.Merchant;
import org.apache.ibatis.annotations.Mapper;

@Mapper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sakurapuare.flightmanagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sakurapuare.flightmanagement.pojo.entity.Passenger;
import com.sakurapuare.flightmanagement.pojo.entity.user.Passenger;
import org.apache.ibatis.annotations.Mapper;

@Mapper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sakurapuare.flightmanagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sakurapuare.flightmanagement.pojo.entity.Staff;
import com.sakurapuare.flightmanagement.pojo.entity.user.Staff;
import org.apache.ibatis.annotations.Mapper;

@Mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sakurapuare.flightmanagement.pojo.entity.User;
import com.sakurapuare.flightmanagement.pojo.entity.user.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.sakurapuare.flightmanagement.pojo.dto.auth.login;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

@Data
public class UserLoginDTO {
@NotBlank(message = "Username is required")
String username;
@NotBlank(message = "Password is required")
String password;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.sakurapuare.flightmanagement.pojo.dto.auth.register;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;

@Data
public class BaseUserRegisterDTO {
@NotBlank(message = "Username is required")
public String username;

@NotBlank(message = "Password is required")
public String password;

@Email(message = "Email is invalid")
public String email;

// @Size(min = 0, max = UserType.ALL, message = "Role is invalid")
// public int role;
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package com.sakurapuare.flightmanagement.pojo.dto.auth.register;

import com.sakurapuare.flightmanagement.constant.UserType;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

@Data
public class UserRegisterDTO {
@NotBlank(message = "Username is required")
public String username;
@NotBlank(message = "Password is required")
public String password;
@Email(message = "Email is invalid")
public String email;
@Size(min = 0, max = UserType.ALL, message = "Role is invalid")
public int role;
public class UserRegisterDTO<T> extends BaseUserRegisterDTO {
@NotNull(message = "Merchant info is required")
T data;
}

This file was deleted.

Loading

0 comments on commit 22592fc

Please sign in to comment.