Skip to content

Commit

Permalink
add experience save, query, delete
Browse files Browse the repository at this point in the history
  • Loading branch information
倪诗梦 authored and 倪诗梦 committed Sep 1, 2024
1 parent 6929075 commit e28873e
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 0 deletions.
73 changes: 73 additions & 0 deletions internal/resume/internal/domain/experience.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023 ecodeclub

Check failure on line 1 in internal/resume/internal/domain/experience.go

View workflow job for this annotation

GitHub Actions / lint

: import cycle not allowed: import stack: [github.com/ecodeclub/webook github.com/ecodeclub/webook/ioc github.com/ecodeclub/webook/internal/resume github.com/ecodeclub/webook/internal/resume/internal/repository github.com/ecodeclub/webook/internal/resume/internal/domain github.com/ecodeclub/webook/internal/resume/internal/web github.com/ecodeclub/webook/internal/resume/internal/domain] (typecheck)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package domain

import (
"time"

"github.com/ecodeclub/webook/internal/resume/internal/web"
)

// Experience 代表工作经历
type Experience struct {
// 主键
Id int64
// 用户的 ID
Uid int64
// 开始时间
Start time.Time
// 结束时间,如果 End 是零值,代表当前还没离职
End time.Time

Title string // 职位
CompanyName string // 公司
Location string // 地点
// JSON 串存起来就可以
Responsibilities []Responsibility // 主要职责
Accomplishments []Accomplishment // 主要成就
Skills []string // 主要技能
}

type Responsibility struct {
// Type 是类型,比如说核心研发、团队管理
// 用 string 来作为枚举
Type string
Content string
}

type Accomplishment struct {
// Type 是类型,比如说性能优化,获奖啥的
Type string
Content string
}

// CurrentEmployed 是否当前正在职
func (e Experience) CurrentEmployed() bool {
return e.End.IsZero()
}

func newResponsibility(responsibilities web.Responsibility) Responsibility {
return Responsibility{
Type: responsibilities.Type,
Content: responsibilities.Content,
}
}

func newAccomplishments(responsibilities web.Accomplishment) Accomplishment {
return Accomplishment{
Type: responsibilities.Type,
Content: responsibilities.Content,
}
}
59 changes: 59 additions & 0 deletions internal/resume/internal/repository/dao/experience.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dao

import (
"context"
"errors"
"time"

"github.com/ego-component/egorm"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type ExperienceDAO interface {
Upsert(ctx context.Context, experience Experience) (int64, error)
Delete(ctx context.Context, uid int64, id int64) error
Find(ctx context.Context, uid int64) ([]Experience, error)
}

type experienceDAO struct {
db *egorm.Component
}

func NewExperienceDAO(db *egorm.Component) ExperienceDAO {
return &experienceDAO{
db: db,
}
}

func (e *experienceDAO) Upsert(ctx context.Context, experience Experience) (int64, error) {
now := time.Now().UnixMilli()
experience.Utime = now
experience.Ctime = now
err := e.db.WithContext(ctx).Model(&Experience{}).
Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "uid"}},
DoUpdates: clause.AssignmentColumns([]string{"start_time", "end_time", "title", "company_name", "location", "responsibilities", "accomplishments", "skills", "utime"}),
}).Create(&experience).Error
return experience.ID, err
}

func (e *experienceDAO) Delete(ctx context.Context, uid int64, id int64) error {

return e.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
res := tx.WithContext(ctx).Model(&Experience{}).Where("id = ? and uid = ?", id, uid).Delete(&Experience{})
if res.Error != nil {
return res.Error
}
if res.RowsAffected <= 0 {
return errors.New("删除失败")
}
return nil
})
}

func (e *experienceDAO) Find(ctx context.Context, uid int64) ([]Experience, error) {
var experiences []Experience
err := e.db.WithContext(ctx).Where("uid = ?", uid).Order("StartTime desc").Find(&experiences).Error
return experiences, err
}
19 changes: 19 additions & 0 deletions internal/resume/internal/repository/dao/type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dao

import "time"

// 简历上的项目
type ResumeProject struct {
ID int64 `gorm:"primaryKey,autoIncrement"`
Expand Down Expand Up @@ -50,3 +52,20 @@ type RefCase struct {
Utime int64
Ctime int64
}

type Experience struct {
ID int64 `gorm:"primaryKey,autoIncrement"`
Uid int64 `gorm:"not null;index"`
// 工作经历开始时间
StartTime time.Time `gorm:"type:date;not null;comment:开始时间"`
// 工作经历结束时间
EndTime time.Time `gorm:"type:date;not null;comment:结束时间"`
Title string `gorm:"type:varchar(255);not null"`
CompanyName string `gorm:"type:varchar(255);not null"`
Location string `gorm:"type:varchar(255);not null"`
Responsibilities string `gorm:"type:text;not null"`
Accomplishments string `gorm:"type:text;not null"`
Skills string `gorm:"type:text;not null"`
Utime int64
Ctime int64
}
99 changes: 99 additions & 0 deletions internal/resume/internal/repository/experience.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package repository

Check failure on line 1 in internal/resume/internal/repository/experience.go

View workflow job for this annotation

GitHub Actions / lint

: import cycle not allowed: import stack: [github.com/ecodeclub/webook github.com/ecodeclub/webook/ioc github.com/ecodeclub/webook/internal/resume github.com/ecodeclub/webook/internal/resume/internal/repository github.com/ecodeclub/webook/internal/resume/internal/domain github.com/ecodeclub/webook/internal/resume/internal/web github.com/ecodeclub/webook/internal/resume/internal/service github.com/ecodeclub/webook/internal/resume/internal/repository] (typecheck)

import (
"context"
"encoding/json"

"github.com/ecodeclub/webook/internal/resume/internal/domain"
"github.com/ecodeclub/webook/internal/resume/internal/repository/dao"
)

type Experience interface {
SaveExperience(ctx context.Context, experience domain.Experience) (int64, error)
QueryAllExperiences(ctx context.Context, uid int64) ([]domain.Experience, error)
DeleteExperience(ctx context.Context, uid int64, id int64) error
}

type experience struct {
expdao dao.ExperienceDAO
}

func (e *experience) SaveExperience(ctx context.Context, experience domain.Experience) (int64, error) {
return e.expdao.Upsert(ctx, e.toExperienceEntity(experience))
}

func (e *experience) QueryAllExperiences(ctx context.Context, uid int64) ([]domain.Experience, error) {
elist, err := e.expdao.Find(ctx, uid)
if err != nil {
return nil, err
}

ans := make([]domain.Experience, 0, len(elist))
for _, exp := range elist {
ans = append(ans, e.toExperienceDomain(exp))
}
return ans, nil
}

func (e *experience) DeleteExperience(ctx context.Context, uid int64, id int64) error {
return e.expdao.Delete(ctx, uid, id)
}

func (e *experience) toExperienceEntity(experience domain.Experience) dao.Experience {
responsibilitiesJsonData, err := json.Marshal(experience.Responsibilities)
if err != nil {
responsibilitiesJsonData = nil
}
accomplishmentsJsonData, err := json.Marshal(experience.Accomplishments)
if err != nil {
accomplishmentsJsonData = nil
}
skillsJsonData, err := json.Marshal(experience.Skills)
if err != nil {
skillsJsonData = nil
}

return dao.Experience{
ID: experience.Id,
StartTime: experience.Start,
EndTime: experience.End,
Title: experience.Title,
CompanyName: experience.CompanyName,
Location: experience.Location,
Responsibilities: string(responsibilitiesJsonData),
Accomplishments: string(accomplishmentsJsonData),
Skills: string(skillsJsonData),
}
}

func (e *experience) toExperienceDomain(experience dao.Experience) domain.Experience {
var responsibilities []domain.Responsibility
err := json.Unmarshal([]byte(experience.Responsibilities), &responsibilities)
if err != nil {
responsibilities = nil
}

var accomplishments []domain.Accomplishment
err = json.Unmarshal([]byte(experience.Accomplishments), &accomplishments)
if err != nil {
accomplishments = nil
}

var skills []string
err = json.Unmarshal([]byte(experience.Skills), &skills)
if err != nil {
skills = nil
}

return domain.Experience{
Id: experience.ID,
Start: experience.StartTime,
End: experience.EndTime,
Title: experience.Title,
CompanyName: experience.CompanyName,
Location: experience.Location,
Responsibilities: responsibilities,
Accomplishments: accomplishments,
Skills: skills,
}
}
36 changes: 36 additions & 0 deletions internal/resume/internal/service/experience.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package service

import (
"context"

"github.com/ecodeclub/webook/internal/resume/internal/domain"

Check failure on line 6 in internal/resume/internal/service/experience.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/ecodeclub/webook/internal/resume/internal/domain (-: import cycle not allowed: import stack: [github.com/ecodeclub/webook github.com/ecodeclub/webook/ioc github.com/ecodeclub/webook/internal/resume github.com/ecodeclub/webook/internal/resume/internal/repository github.com/ecodeclub/webook/internal/resume/internal/domain github.com/ecodeclub/webook/internal/resume/internal/web github.com/ecodeclub/webook/internal/resume/internal/domain]) (typecheck)
"github.com/ecodeclub/webook/internal/resume/internal/repository"

Check failure on line 7 in internal/resume/internal/service/experience.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/ecodeclub/webook/internal/resume/internal/repository (-: import cycle not allowed: import stack: [github.com/ecodeclub/webook github.com/ecodeclub/webook/ioc github.com/ecodeclub/webook/internal/resume github.com/ecodeclub/webook/internal/resume/internal/repository github.com/ecodeclub/webook/internal/resume/internal/domain github.com/ecodeclub/webook/internal/resume/internal/web github.com/ecodeclub/webook/internal/resume/internal/service github.com/ecodeclub/webook/internal/resume/internal/repository]) (typecheck)
)

type ExperienceService interface {
SaveExperience(ctx context.Context, experience domain.Experience) (int64, error)
List(ctx context.Context, uid int64) ([]domain.Experience, error)
Delete(ctx context.Context, uid int64, id int64) error
}

type experienceService struct {
experience repository.Experience
}

//func NewExperienceService(experience repository.Experience) ExperienceService {
// return &experienceService{
// experience: experience,
// }
//}

func (e *experienceService) SaveExperience(ctx context.Context, experience domain.Experience) (int64, error) {
return e.experience.SaveExperience(ctx, experience)
}

func (e *experienceService) List(ctx context.Context, uid int64) ([]domain.Experience, error) {
return e.experience.QueryAllExperiences(ctx, uid)
}

func (e *experienceService) Delete(ctx context.Context, uid int64, id int64) error {
return e.experience.DeleteExperience(ctx, uid, id)
}
Loading

0 comments on commit e28873e

Please sign in to comment.