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

Commit

Permalink
Merge 1f7ce52 into fa06b9f
Browse files Browse the repository at this point in the history
  • Loading branch information
Huangscar committed Oct 31, 2018
2 parents fa06b9f + 1f7ce52 commit ba63f64
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ $ agenda meeting set -t <title> [-s <startTime>] [-e <endTime>] [-p <participato

### Add participators

```sh
$ agenda meeting add -t <title> participators
```

- You can use `a` as an alias for `add`.
- If you want to add more than one participator, please input like `p1 p2 p3 ...` .
- You must be the initiator of this meeting.
- If you add the participator who has been added into the meeting, you will not get the error message but this participator will not be add to the meeting twice.

### Remove participators

```
Expand Down
9 changes: 9 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ $ agenda meeting set -t <title> [-s <startTime>] [-e <endTime>] [-p <participato

### 添加与会者

```sh
$ agenda meeting add -t <title> participators
```

- 你可以用 `a` 代替 `add`
- 如果你想要添加多个与会者,请像这样输入: `p1 p2 p3 ...`
- 你必须是这个会议的发起者。
- 如果你添加了原来会议就存在的成员,你将不会收到错误信息,但是这个人不会再次加入到会议中。

### 移除与会者

```
Expand Down
19 changes: 13 additions & 6 deletions controller/meeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package controller

import (
"fmt"
"github.com/MegaShow/goagenda/lib/log"

"time"

"github.com/MegaShow/goagenda/lib/log"
)

type MeetingCtrl interface {
Expand Down Expand Up @@ -149,14 +151,19 @@ func (c *Controller) MeetingDelete() {

func (c *Controller) MeetingAdd() {
title, _ := c.Ctx.GetString("title")
fmt.Println("title: ", title)
participator := c.Args
if len(participator) == 0 {
fmt.Println(0)
user := c.Ctx.User.Get()
if user == "" {
fmt.Println("you should first login")
return
}
for i := 0; i < len(participator); i++ {
fmt.Println(participator[i])
err := ctrl.Srv.Meeting().AddMeeting(title, participator, user)
if err != nil {
log.Error(err.Error())
} else {
log.Info("add successful")
}

}

func (c *Controller) MeetingList() {
Expand Down
25 changes: 23 additions & 2 deletions model/meeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type MeetingModel interface {
GetMeetingsByUser(user string) []Meeting
GetOccupiedParticipators(title string, startTime, endTime time.Time) map[string]bool
CreateMeeting(meeting Meeting)
AddMeeting(title string, participators []string)
SetMeeting(title string, startTime time.Time, setStart bool, endTime time.Time, setEnd bool, participators []string, setPars bool)
DeleteMeetingByTitle(title string) bool
DeleteMeetingsByInitiator(name string) int
Expand Down Expand Up @@ -59,7 +60,7 @@ func (m *MeetingDB) GetMeetingsByUser(user string) (res []Meeting) {
return
}

func (m *MeetingDB) GetOccupiedParticipators(title string, startTime, endTime time.Time) map[string]bool{
func (m *MeetingDB) GetOccupiedParticipators(title string, startTime, endTime time.Time) map[string]bool {
occupiedParticipators := make(map[string]bool)
for _, item := range m.Data {
if item.Title != title && item.EndTime.After(startTime) && item.StartTime.Before(endTime) {
Expand All @@ -77,6 +78,26 @@ func (m *MeetingDB) CreateMeeting(meeting Meeting) {
m.Data = append(m.Data, meeting)
}

func (m *MeetingDB) AddMeeting(title string, participators []string) {
m.isDirty = true
for index, item := range m.Data {
if item.Title == title {
participatorMap := make(map[string]bool)
for _, participator := range item.Participators {
participatorMap[participator] = true
}
for _, participator := range participators {
_, hasAdd := participatorMap[participator]
if !hasAdd {
participatorMap[participator] = true
m.Data[index].Participators = append(item.Participators, participator)
}
}
break
}
}
}

func (m *MeetingDB) SetMeeting(title string, startTime time.Time, setStart bool,
endTime time.Time, setEnd bool, participators []string, setPars bool) {
m.isDirty = true
Expand All @@ -95,7 +116,7 @@ func (m *MeetingDB) SetMeeting(title string, startTime time.Time, setStart bool,
}
}
}

func (m *MeetingDB) DeleteMeetingByTitle(title string) bool {
m.isDirty = true
for i := 0; i < len(m.Data); i++ {
Expand Down
40 changes: 37 additions & 3 deletions service/meeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package service

import (
"errors"
"github.com/MegaShow/goagenda/lib/log"
"github.com/MegaShow/goagenda/model"
"sort"
"strings"
"time"

"github.com/MegaShow/goagenda/lib/log"
"github.com/MegaShow/goagenda/model"
)

type MeetingService interface {
Expand All @@ -16,6 +17,7 @@ type MeetingService interface {
QuitMeeting(user, title string) error
RemoveParticipators(user, title string, participators []string) error
ListMeetings(user, title string, startTime, endTime time.Time) (string, error)
AddMeeting(title string, participators []string, name string) error
}

func CheckFreeParticipators(participators []string, initiator string, occupiedParticipators map[string]bool) (bool, string) {
Expand Down Expand Up @@ -84,6 +86,38 @@ func (s *Service) CreateMeeting(title string, startTime time.Time, endTime time.
return nil
}

func (s *Service) AddMeeting(title string, participators []string, name string) error {
log.Verbose("check if meeting is exit")
meeting := s.DB.Meeting().GetMeetingByTitle(title)
if meeting.Title == "" {
return errors.New("meeting with title \"" + title + "\" doesn't exit")
}

log.Verbose("check if the user is the sponsor of the meeting")
if meeting.Initiator != name {
return errors.New("you can only add participators to your meeting")
}

log.Verbose("check if some participator doesn't exist")
for _, participator := range participators {
if s.DB.User().GetUserByName(participator).Name == "" {
return errors.New("user \"" + participator + "\" doesn't exist")
}
}

log.Verbose("check if some participator is occupied")
occupiedParticipators := s.DB.Meeting().GetOccupiedParticipators(title, meeting.StartTime, meeting.EndTime)
free, occupiedOne := CheckFreeParticipators(participators, "", occupiedParticipators)
if !free {
var begin string
begin = "user '" + occupiedOne + "' is"
return errors.New(begin + " occupied during the time")
}

s.DB.Meeting().AddMeeting(title, participators)
return nil
}

func (s *Service) SetMeeting(title string, startTime time.Time, setStart bool, endTime time.Time, setEnd bool,
initiator string, participators []string, setPars bool) error{
log.Verbose("check if meeting exists")
Expand Down Expand Up @@ -118,7 +152,7 @@ func (s *Service) SetMeeting(title string, startTime time.Time, setStart bool, e
if s.DB.User().GetUserByName(participator).Name == "" {
return errors.New("user '" + participator + "' doesn't exist")
}
}
}
}

log.Verbose("check if some participator is occupied")
Expand Down

0 comments on commit ba63f64

Please sign in to comment.