github.com/infraboard/keyauth@v0.8.1/apps/application/impl/user.go (about) 1 package impl 2 3 import ( 4 "context" 5 6 "github.com/infraboard/mcube/exception" 7 "github.com/infraboard/mcube/http/request" 8 "go.mongodb.org/mongo-driver/bson" 9 "go.mongodb.org/mongo-driver/mongo" 10 11 "github.com/infraboard/keyauth/apps/application" 12 ) 13 14 type userimpl struct { 15 *service 16 application.UnimplementedServiceServer 17 } 18 19 func (s *userimpl) CreateApplication(ctx context.Context, req *application.CreateApplicatonRequest) ( 20 *application.Application, error) { 21 app, err := application.NewUserApplicartion(req) 22 if err != nil { 23 return nil, err 24 } 25 26 return s.save(app) 27 } 28 29 func (s *userimpl) DescribeApplication(ctx context.Context, req *application.DescribeApplicationRequest) ( 30 *application.Application, error) { 31 r, err := newDescribeQuery(req) 32 if err != nil { 33 return nil, err 34 } 35 36 app := new(application.Application) 37 if err := s.col.FindOne(context.TODO(), r.FindFilter()).Decode(app); err != nil { 38 if err == mongo.ErrNoDocuments { 39 return nil, exception.NewNotFound("applicaiton %s not found", req) 40 } 41 42 return nil, exception.NewInternalServerError("find application %s error, %s", req.Id, err) 43 } 44 45 return app, nil 46 } 47 48 func (s *userimpl) QueryApplication(ctx context.Context, req *application.QueryApplicationRequest) (*application.Set, error) { 49 r := newPaggingQuery(req) 50 resp, err := s.col.Find(context.TODO(), r.FindFilter(), r.FindOptions()) 51 52 if err != nil { 53 return nil, exception.NewInternalServerError("find domain error, error is %s", err) 54 } 55 56 appSet := application.NewApplicationSet(request.NewPageRequest(uint(req.Page.PageSize), uint(req.Page.PageNumber))) 57 // 循环 58 for resp.Next(context.TODO()) { 59 app := new(application.Application) 60 if err := resp.Decode(app); err != nil { 61 return nil, exception.NewInternalServerError("decode domain error, error is %s", err) 62 } 63 64 appSet.Add(app) 65 } 66 67 // count 68 count, err := s.col.CountDocuments(context.TODO(), r.FindFilter()) 69 if err != nil { 70 return nil, exception.NewInternalServerError("get device count error, error is %s", err) 71 } 72 appSet.Total = count 73 74 return appSet, nil 75 } 76 77 func (s *userimpl) DeleteApplication(ctx context.Context, req *application.DeleteApplicationRequest) (*application.Application, error) { 78 result, err := s.col.DeleteOne(context.TODO(), bson.M{"_id": req.Id}) 79 if err != nil { 80 return nil, exception.NewInternalServerError("delete application(%s) error, %s", req.Id, err) 81 } 82 83 if result.DeletedCount == 0 { 84 return nil, exception.NewNotFound("app %s not found", req.Id) 85 } 86 return nil, nil 87 }