Skip to content

Commit

Permalink
Update Codes
Browse files Browse the repository at this point in the history
  • Loading branch information
onemsg committed Sep 30, 2024
1 parent dceb78c commit dc9fc73
Show file tree
Hide file tree
Showing 29 changed files with 796 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.onemsg.authorization;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;

public class ObservationTest {


public static void main(String[] args) {
// ObservationRegistry registry = ObservationRegistry.create();

// Observation.createNotStarted("foo", registry)
// .lowCardinalityKeyValue("lowTag", "lowTagValue")
// .highCardinalityKeyValue("highTag", "highTagValue")
// .observe(() -> System.out.println("Hello"));

MeterRegistry registry = new SimpleMeterRegistry();
Timer.Sample sample = Timer.start(registry);
try {
// do some work here
} finally {
sample.stop(Timer.builder("my.timer").register(registry));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.onemsg.authorization.controller;

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.RestController;

@RestController
@RequestMapping("/api/authorization")
public class AuthController {

@GetMapping("roles")
public ResponseEntity<Object> getRoles() {
return null;
}

@GetMapping("perms")
public ResponseEntity<Object> getPerms() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.onemsg.protobuf.manager.application.event;

import org.springframework.context.ApplicationEvent;

public class RefreshGroupStoreEvent extends ApplicationEvent{

public RefreshGroupStoreEvent(Object source) {
super(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.onemsg.protobuf.manager.application.model;

import java.util.Objects;

import org.springframework.beans.BeanUtils;

public class ApplicationCreation {
public String name;
public String zhName;
public String intro;
public int groupId;
public String groupName;
public String creator;

public String getFullName() {
return groupName + "/" + name;
}

public static ApplicationCreation create(ApplicationCreationRequest request) {
Objects.requireNonNull(request);
ApplicationCreation creation = new ApplicationCreation();
BeanUtils.copyProperties(request, creation);
return creation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.onemsg.protobuf.manager.application.model;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;

public class ApplicationCreationRequest {

@NotBlank
public String name;
public String zhName;
public String intro;
@Positive
public int groupId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.onemsg.protobuf.manager.application.model;

import java.time.LocalDateTime;

public class ApplicationEntity {

public int id;
public String name;
public String zhName;
public String intro;
public int groupId;
public String groupName;
public String creator;
public LocalDateTime createdTime;
public LocalDateTime updatedTime;

public int getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.onemsg.protobuf.manager.application.model;

public record ApplicationNameVo(
int id,
String name
) {

public static ApplicationNameVo create(ApplicationEntity entity) {
return new ApplicationNameVo(entity.id, entity.name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.onemsg.protobuf.manager.application.model;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;

public class ApplicationUpdateRequest {
@Positive
public int applicationId;
@NotBlank
public String zhName;
public String intro;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.onemsg.protobuf.manager.application.model;

import jakarta.validation.constraints.NotBlank;

public class GroupCreationRequest {
@NotBlank
public String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.onemsg.protobuf.manager.application.model;

import java.time.LocalDateTime;

/**
* ApplicationEntity
*/
public record GroupEntity(
int id,
String name,
String creator,
LocalDateTime create
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.onemsg.protobuf.manager.application.model;

public class GroupInsertion {
public String name;
public String creator;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.onemsg.protobuf.manager.application.model;

public record GroupNameVo(int id, String name) {

public static GroupNameVo create(GroupEntity entity) {
return new GroupNameVo(entity.id(), entity.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.onemsg.protobuf.manager.model;

import jakarta.validation.constraints.Positive;

public class RemoveIdRequest {
@Positive
public int id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package com.onemsg.protobuf.manager.protobuf;

import java.util.List;
import java.util.Optional;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeCreation;
import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeEntity;
import com.onemsg.protobuf.manager.protobuf.model.ProtobufCodeVersion;
import com.onemsg.protobuf.manager.protobuf.model.ProtobufInfoCreation;
import com.onemsg.protobuf.manager.protobuf.model.ProtobufInfoEntity;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Repository
public class ProtobufInfoRepository {

private final JdbcClient jdbcClient;

public ProtobufInfoRepository(JdbcClient jdbcClient) {
this.jdbcClient = jdbcClient;
}

public List<ProtobufInfoEntity> search(String search, int skip, int limit) {
String sql = """
SELECT id, name, intro, application_name group_name, protocol, current_version, creator, created_time, updated_time
FROM `protobuf_info`
WHERE group_name like ? OR application_name like ? OR name like ? OR creator like ?
ORDER BY id DESC
LIMIT ?,?
""";
String word = "%" + search + "%";
return jdbcClient.sql(sql).params(word, word, word, word, skip, limit)
.query(ProtobufInfoEntity.class)
.list();
}


public int count(String search) {
String sql = """
SELECT count(id) FROM `protobuf_info`
WHERE group_name like ? OR application_name like ? OR name like ?
""";
String word = "%" + search + "%";
return jdbcClient.sql(sql)
.params(word, word, word)
.query(int.class)
.single();
}

public List<ProtobufInfoEntity> search(int skip, int limit) {
String sql = """
SELECT id, name, intro, application_name group_name, protocol, current_version, creator, created_time, updated_time
FROM `protobuf_info`
ORDER BY id DESC
LIMIT ?,?
""";

return jdbcClient.sql(sql).params(skip, limit).query(ProtobufInfoEntity.class).list();
}

public int count() {
String sql = "SELECT count(id) FROM `protobuf_info`";
return jdbcClient.sql(sql).query(int.class).single();
}

public Optional<ProtobufInfoEntity> findById(int id) {
String sql = """
SELECT id, name, intro, application_name group_name, protocol, current_version, creator, created_time, updated_time
FROM `protobuf_info` WHERE id = ?
""";
return jdbcClient.sql(sql).param(id).query(ProtobufInfoEntity.class).optional();
}

public boolean existsById(int id) {
String sql = "SELECT count(id) FROM `protobuf_info` WHERE id = ?";
return jdbcClient.sql(sql).param(id).query(int.class).single() > 0;
}

public int updateIntroById(int id, String intro) {
String sql = "UPDATE `protobuf_info` SET `intro` = ? WHERE `id` = ?";
return jdbcClient.sql(sql).param(intro, id).update();
}

public int insert(ProtobufInfoCreation creation) {
String sql = """
INSERT INTO `protobuf_info`(name, intro, application_name, group_name, protocol, creator)
VALUES(:name, :intro, :applicationName, :groupName, :protocol, :creator);
""";

log.info("Insert {}", creation);

KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcClient.sql(sql)
.paramSource(creation)
.update(keyHolder, "id");

return getKeyAsInt(keyHolder, "protobuf_info");
}


private static int getKeyAsInt(KeyHolder keyHolder, String tableName) {
var key = keyHolder.getKey();
if (key == null) {
String msg = String.format("获取 Table[%s] 自增ID失败", tableName);
throw new DataAccessException(msg) {};
}
return key.intValue();
}

public int delete(int id) {
String sql = "DELETE FROM `protobuf_info` WHERE id = ?";
return jdbcClient.sql(sql).param(id).update();
}

public int insertCode(ProtobufCodeCreation creation) {

int nextVersion = jdbcClient.sql("SELECT _next_version FROM `protobuf_info` WHERE id = ? FOR UPDATE")
.param(creation.protobufId)
.query(int.class)
.single();

creation.version = nextVersion;

String insertSql = """
INSERT INTO `protobuf_code`(protobuf_id, protobuf_name, code, version, creator)
VALUES(:protobufId, :protobufName, :code, :version, :creator );
""";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcClient.sql(insertSql).paramSource(creation).update(keyHolder, "id");

String updateSql = "UPDATE `protobuf_info` SET current_version = ?, _next_version = _next_version + 1 WHERE id = ?";
jdbcClient.sql(updateSql)
.param(nextVersion)
.param(creation.protobufId)
.update();

return getKeyAsInt(keyHolder, "protobuf_code");
}

public int updateCurrentVersion(int id, int version) {
String updateSql = "UPDATE `protobuf_info` SET current_version = ? WHERE id = ?";
return jdbcClient.sql(updateSql).param(version, id).update();
}

public Optional<ProtobufCodeEntity> findCodeById(int codeId) {
String sql = """
SELECT id, protobuf_id, protobuf_name, code, version, creator, created_time
FROM `protobuf_code` WHERE id = ?
""";
return jdbcClient.sql(sql).param(codeId).query(ProtobufCodeEntity.class).optional();
}

public Optional<ProtobufCodeEntity> findCodeByProtobufIdAndVersion(int protobufId, int version) {
String sql = """
SELECT id, protobuf_id, protobuf_name, code, version, creator, created_time
FROM `protobuf_code`
WHERE protobuf_id = ? AND version = ?
""";
return jdbcClient.sql(sql).param(protobufId, version).query(ProtobufCodeEntity.class).optional();
}

public Optional<ProtobufCodeEntity> findCurrentCodeByProtobufId(int protobufId) {
String sql = """
SELECT id, protobuf_id, protobuf_name, code, version, creator, created_time
FROM `protobuf_code`
WHERE protobuf_id = ?
AND version in (SELECT current_version FROM `current_version` WHERE id = ?)
""";
return jdbcClient.sql(sql).param(protobufId, protobufId).query(ProtobufCodeEntity.class).optional();
}

public boolean existsCodeByProtobufIdAndVersion(int protobufId, int version) {
String sql = """
SELECT count(id)
FROM `protobuf_code`
WHERE protobuf_id = ? AND version = ?
""";
return jdbcClient.sql(sql).param(protobufId, version).query(int.class).single() > 0;
}

public List<ProtobufCodeVersion> findCodeVersionListByProtobufId(int protobufId) {
String sql = """
SELECT code.id, code.version, code.version = info.current_version as is_current, code.creator, code.created_time
FROM `protobuf_code` as code
LEFT JOIN `protobuf_info` as info ON code.protobuf_id = info.id
WHERE code.protobuf_id = ?
""";
return jdbcClient.sql(sql).param(protobufId).query(ProtobufCodeVersion.class).list();
}

}
Loading

0 comments on commit dc9fc73

Please sign in to comment.