Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Admissions Promotion Management System Using Java, Spring Boot, and MySQL

Tech 1

Technology Stack

The system is developed using Java with the Spring Boot framework. The frontend utilizes JavaScript libraries such as jQuery and Ajax for dynamic interactions. MySQL serves as the backend database for storing all application data.

Functional Overview

Administrator Features

Authentication

Administrators access the system through a login interface requiring a valid username and password. Successful authentication grants entry to the administrative dashboard.

Recruitment Plan Management

This section allows administrators to perform CRUD operations on recruitmnet plans, including viewing, creating, editing, and deleting entries.

Announcement Management

Admins can manage announcements by creating new posts, modifying existing ones, or removing outdated information from the system.

Announcement Category Mnaagement

The system supports managing announcement categories, enabling admins to add new types, modify current classifications, or remove obsolete categories.

Core Implementation

Authentication Controller

package com.app.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.app.entity.Admin;
import com.app.service.AuthService;
import com.app.service.SessionTokenService;
import com.app.util.ResponseBuilder;

@RestController
@RequestMapping("/auth")
class AuthController {
    
    @Autowired
    private AuthService adminService;
    
    @Autowired
    private SessionTokenService tokenProvider;
    
    @PostMapping("/signin")
    public ResponseBuilder authenticate(
        @RequestParam String user,
        @RequestParam String pass,
        HttpServletRequest req) {
        
        Admin account = adminService.fetchByField("username", user);
        if(account == null || !account.getPassHash().equals(pass)) {
            return ResponseBuilder.failure("Invalid credentials");
        }
        
        String jwt = tokenProvider.create(account.getId(), user, "admin", account.getRole());
        return ResponseBuilder.success().addData("token", jwt);
    }
    
    @PostMapping("/signup")
    public ResponseBuilder registerAccount(@RequestBody Admin newAdmin) {
        if(adminService.fetchByField("username", newAdmin.getUsername()) != null) {
            return ResponseBuilder.failure("Username already exists");
        }
        adminService.addNew(newAdmin);
        return ResponseBuilder.success();
    }
    
    @GetMapping("/signout")
    public ResponseBuilder endSession(HttpServletRequest req) {
        req.getSession().invalidate();
        return ResponseBuilder.success("Logged out successfully");
    }
}

File Handling Component

package com.app.controller;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired;
import com.app.config.SystemSettings;
import com.app.util.FileUtility;
import com.app.util.ResponseBuilder;

@RestController
@RequestMapping("/storage")
class StorageController {
    
    @Autowired
    private SystemSettings settings;
    
    @PostMapping("/upload")
    public ResponseBuilder saveFile(
        @RequestParam("document") MultipartFile uploadedFile,
        @RequestParam(required = false) String category) throws IOException {
        
        if (uploadedFile.isEmpty()) {
            throw new IllegalArgumentException("No file selected");
        }
        
        String extension = getFileExtension(uploadedFile.getOriginalFilename());
        Path storagePath = Paths.get("uploads");
        
        if (!Files.exists(storagePath)) {
            Files.createDirectories(storagePath);
        }
        
        String uniqueName = UUID.randomUUID().toString() + "." + extension;
        Path targetLocation = storagePath.resolve(uniqueName);
        uploadedFile.transferTo(targetLocation.toFile());
        
        if("avatar".equals(category)) {
            settings.updateSetting("profileImage", uniqueName);
        }
        
        return ResponseBuilder.success().addData("filename", uniqueName);
    }
    
    private String getFileExtension(String fullName) {
        int lastIndex = fullName.lastIndexOf('.');
        return lastIndex > 0 ? fullName.substring(lastIndex + 1) : "";
    }
}

Response Utility Class

package com.app.util;

import java.util.HashMap;

public class ResponseBuilder extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    
    public ResponseBuilder() {
        put("status", 200);
    }
    
    public static ResponseBuilder failure() {
        return failure(500, "An unexpected error occurred");
    }
    
    public static ResponseBuilder failure(String message) {
        return failure(500, message);
    }
    
    public static ResponseBuilder failure(int statusCode, String message) {
        ResponseBuilder rb = new ResponseBuilder();
        rb.put("status", statusCode);
        rb.put("message", message);
        return rb;
    }

    public static ResponseBuilder success(String msg) {
        ResponseBuilder rb = new ResponseBuilder();
        rb.put("message", msg);
        return rb;
    }
    
    public static ResponseBuilder success() {
        return new ResponseBuilder();
    }
    
    public ResponseBuilder addData(String key, Object value) {
        super.put(key, value);
        return this;
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.