Browse Source

新建启智项目申请页面提交

2023
chenyifan01 2 years ago
parent
commit
73c4d22fdd
5 changed files with 116 additions and 2 deletions
  1. +30
    -0
      models/tech_converge_info.go
  2. +11
    -0
      modules/structs/tech.go
  3. +1
    -1
      routers/api/v1/api.go
  4. +34
    -1
      routers/api/v1/tech/repo.go
  5. +40
    -0
      services/repository/url.go

+ 30
- 0
models/tech_converge_info.go View File

@@ -8,6 +8,14 @@ import (
"xorm.io/builder"
)

const (
TechHide = 1
TechShow = 2
TechMigrateFailed = 3
)

const DefaultTechStatus = 2

type TechConvergeBaseInfo struct {
ID int64 `xorm:"pk autoincr"`
ProjectNumber string `xorm:"UNIQUE NOT NULL"` //项目立项编号
@@ -66,6 +74,16 @@ type RepoConvergeInfo struct {
BaseInfo *TechConvergeBaseInfo `xorm:"-"`
}

func (r *RepoConvergeInfo) InsertOrUpdate() error {
if r.ID != 0 {
_, err := x.ID(r.ID).Update(r)
return err
} else {
_, err := x.InsertOne(r)
return err
}
}

func GetTechConvergeBaseInfoByProjectNumber(projectNumber string) (*TechConvergeBaseInfo, error) {
tb := &TechConvergeBaseInfo{ProjectNumber: projectNumber}
return getTechConvergeBaseInfo(tb)
@@ -139,3 +157,15 @@ func FindTech(opt FindTechOpt) ([]*TechConvergeBaseInfo, error) {
}
return r, nil
}

func GetTechByTechNo(techNo string) (*TechConvergeBaseInfo, error) {
var tech *TechConvergeBaseInfo
has, err := x.Where("project_number = ?", techNo).Get(&tech)
if err != nil {
return nil, err
} else if !has {
return nil, ErrTechConvergeBaseInfoNotExist{}
}
return tech, nil

}

+ 11
- 0
modules/structs/tech.go View File

@@ -0,0 +1,11 @@
package structs

type TechRepo struct {
Url string `json:"url"`
TechNo string `json:"no"`
Institution string `json:"institution"`
uid string `json:"uid"` //启智项目uid
repo_name string `json:"repo_name"` //启智项目名称
topics string `json:"topics"` //关键词
description string `json:"description"` //简介
}

+ 1
- 1
routers/api/v1/api.go View File

@@ -540,7 +540,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/tech", func() {
m.Get("", tech.FindTech)
m.Post("/basic", tech.ImportBasicInfo)
m.Post("/openi", tech.CommitOpenIRepo)
m.Post("/openi", bind(api.TechRepo{}), tech.CommitOpenIRepo)

}, reqToken())



+ 34
- 1
routers/api/v1/tech/repo.go View File

@@ -1,9 +1,42 @@
package tech

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/response"
"code.gitea.io/gitea/services/repository"
"net/http"
)

func CommitOpenIRepo(ctx *context.APIContext) {
func CommitOpenIRepo(ctx *context.APIContext, form api.TechRepo) {
//解析项目路径,查找项目
repo, err := repository.FindRepoByUrl(form.Url)
if err != nil {
ctx.JSON(http.StatusOK, response.OuterServerError(err.Error()))
return
}
//查找项目编号
tech, err := models.GetTechByTechNo(form.TechNo)
if err != nil {
ctx.JSON(http.StatusOK, response.OuterServerError(err.Error()))
return
}

//写入数据库
rci := &models.RepoConvergeInfo{
RepoID: repo.ID,
Url: form.Url,
BaseInfoID: tech.ID,
Institution: form.Institution,
UID: ctx.User.ID,
Status: models.DefaultTechStatus,
}
err = rci.InsertOrUpdate()
if err != nil {
ctx.JSON(http.StatusOK, response.OuterServerError(err.Error()))
return
}
ctx.JSON(http.StatusOK, response.OuterSuccess())
return
}

+ 40
- 0
services/repository/url.go View File

@@ -0,0 +1,40 @@
package repository

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"net/url"
"strings"
)

func FindRepoByUrl(url string) (*models.Repository, error) {
ownerName, repoName := parseOpenIUrl(url)
if ownerName == "" || repoName == "" {
return nil, nil
}
r, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
if err != nil {
return nil, err
}
return r, nil
}

//parseOpenIUrl parse openI repo url,return ownerName and repoName
func parseOpenIUrl(u string) (string, string) {
url, err := url.Parse(u)
if err != nil {
return "", ""
}
if url.Host != setting.AppURL {
return "", ""
}

array := strings.Split(url.Path, "/")
if len(array) < 3 {
return "", ""
}

ownerName := array[1]
repoName := array[2]
return ownerName, repoName
}

Loading…
Cancel
Save