func (m *ProjectManager) Create(raw *project.Project) (*project.Project, error) { // TODO (m0sth8): add validation raw.Id = bson.NewObjectId() raw.Created = time.Now().UTC() raw.Updated = raw.Created if raw.Members == nil { raw.Members = []*project.Member{} } if err := m.col.Insert(raw); err != nil { return nil, err } return raw, nil }
func (s *ProjectService) update(req *restful.Request, resp *restful.Response, p *project.Project) { raw := &ProjectEntity{} if err := req.ReadEntity(raw); err != nil { logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusBadRequest, services.WrongEntityErr) return } user := filters.GetUser(req) if p.Owner != user.Id { resp.WriteServiceError(http.StatusForbidden, services.AuthForbidErr) return } mgr := s.Manager() defer mgr.Close() if raw.Name != "" { p.Name = raw.Name } if err := mgr.Projects.Update(p); err != nil { if mgr.IsDup(err) { resp.WriteServiceError( http.StatusConflict, services.NewError(services.CodeDuplicate, "project with this name and owner is existed")) return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } resp.WriteEntity(p) }
func (s *ProjectService) membersDelete(req *restful.Request, resp *restful.Response, p *project.Project, m *project.Member) { members := make([]*project.Member, 0, len(p.Members)-1) for _, member := range p.Members { if member.User != m.User { members = append(members, member) } } p.Members = members mgr := s.Manager() defer mgr.Close() err := mgr.Projects.Update(p) if err != nil { if mgr.IsNotFound(err) { resp.WriteErrorString(http.StatusNotFound, "Not found") return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } resp.ResponseWriter.WriteHeader(http.StatusNoContent) }
func (s *ProjectService) membersCreate(req *restful.Request, resp *restful.Response, p *project.Project) { raw := &project.Member{} if err := req.ReadEntity(raw); err != nil { logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusBadRequest, services.WrongEntityErr) return } if raw.User == "" { resp.WriteServiceError(http.StatusBadRequest, services.NewBadReq("user is required")) return } u := filters.GetUser(req) if p.Owner != u.Id { resp.WriteServiceError(http.StatusForbidden, services.AuthForbidErr) return } for _, member := range p.Members { if member.User == raw.User { resp.WriteServiceError(http.StatusConflict, services.NewError(services.CodeDuplicate, "User is already member")) return } } mgr := s.Manager() defer mgr.Close() mUser, err := mgr.Users.GetById(raw.User) if err != nil { if mgr.IsNotFound(err) { resp.WriteErrorString(http.StatusNotFound, "User not found") return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } member := &project.Member{User: mUser.Id} p.Members = append(p.Members, member) err = mgr.Projects.Update(p) if err != nil { if mgr.IsNotFound(err) { resp.WriteErrorString(http.StatusNotFound, "Not found") return } logrus.Error(stackerr.Wrap(err)) resp.WriteServiceError(http.StatusInternalServerError, services.DbErr) return } resp.WriteHeader(http.StatusCreated) resp.WriteEntity(member) }
func (m *ProjectManager) Update(obj *project.Project) error { obj.Updated = time.Now().UTC() return m.col.UpdateId(obj.Id, obj) }