Add Contact API (#340)

* feat: add basic files of contact

* chore: code review
1. delete redundant suffix
2. use tab replace whitespace in pb file

* chore: add some comments and check post data validation

* chore: add some comments

* chore: add publicapi.json and examples.json

* chore: update README.md

* fix: code review conversations
This commit is contained in:
zhaoyang
2021-12-21 17:59:45 +08:00
committed by GitHub
parent 0fa23b753f
commit 5f6de1fd18
16 changed files with 2498 additions and 3 deletions

1475
contact/proto/contact.pb.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,161 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/contact.proto
package contact
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Contact service
func NewContactEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Contact service
type ContactService interface {
Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error)
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error)
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error)
}
type contactService struct {
c client.Client
name string
}
func NewContactService(name string, c client.Client) ContactService {
return &contactService{
c: c,
name: name,
}
}
func (c *contactService) Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error) {
req := c.c.NewRequest(c.name, "Contact.Create", in)
out := new(CreateResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *contactService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
req := c.c.NewRequest(c.name, "Contact.Read", in)
out := new(ReadResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *contactService) Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error) {
req := c.c.NewRequest(c.name, "Contact.Update", in)
out := new(UpdateResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *contactService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
req := c.c.NewRequest(c.name, "Contact.Delete", in)
out := new(DeleteResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *contactService) List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error) {
req := c.c.NewRequest(c.name, "Contact.List", in)
out := new(ListResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Contact service
type ContactHandler interface {
Create(context.Context, *CreateRequest, *CreateResponse) error
Read(context.Context, *ReadRequest, *ReadResponse) error
Update(context.Context, *UpdateRequest, *UpdateResponse) error
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
List(context.Context, *ListRequest, *ListResponse) error
}
func RegisterContactHandler(s server.Server, hdlr ContactHandler, opts ...server.HandlerOption) error {
type contact interface {
Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
List(ctx context.Context, in *ListRequest, out *ListResponse) error
}
type Contact struct {
contact
}
h := &contactHandler{hdlr}
return s.Handle(s.NewHandler(&Contact{h}, opts...))
}
type contactHandler struct {
ContactHandler
}
func (h *contactHandler) Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error {
return h.ContactHandler.Create(ctx, in, out)
}
func (h *contactHandler) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
return h.ContactHandler.Read(ctx, in, out)
}
func (h *contactHandler) Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error {
return h.ContactHandler.Update(ctx, in, out)
}
func (h *contactHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
return h.ContactHandler.Delete(ctx, in, out)
}
func (h *contactHandler) List(ctx context.Context, in *ListRequest, out *ListResponse) error {
return h.ContactHandler.List(ctx, in, out)
}

151
contact/proto/contact.proto Normal file
View File

@@ -0,0 +1,151 @@
syntax = "proto3";
package contact;
option go_package = "./proto;contact";
service Contact {
rpc Create(CreateRequest) returns (CreateResponse) {}
rpc Read(ReadRequest) returns (ReadResponse) {}
rpc Update(UpdateRequest) returns (UpdateResponse) {}
rpc Delete(DeleteRequest) returns (DeleteResponse) {}
rpc List(ListRequest) returns (ListResponse) {}
// TODO: wait for the search API
// rpc Search(Request) returns (Response) {}
}
message Phone {
// the label of the phone number
string label = 1;
// phone number
string number = 2;
}
message Email {
// the label of the email
string label = 1;
// the email address
string address = 2;
}
message Link {
// the label of the link
string label = 1;
// the url of the contact
string url = 2;
}
message Address {
// the label of the address
string label = 1;
// the address
string address = 2;
}
message SocialMedia {
// the label of the social
string label = 1;
// the username of social media
string username = 2;
}
message ContactInfo {
// contact id
string id = 1;
// the contact name
string name = 2;
// the phone numbers
repeated Phone phones = 3;
// the emails
repeated Email emails = 4;
// the contact links
repeated Link links = 5;
// the birthday
string birthday = 6;
// the address
repeated Address addresses = 7;
// the social media username
repeated SocialMedia social_medias = 8;
// note of the contact
string note = 9;
// create date string in RFC3339
string created_at = 10;
// update date string in RFC3339
string updated_at = 11;
}
message CreateRequest {
// required, the name of the contact
string name = 1;
// optional, phone numbers
repeated Phone phones = 2;
// optional, emails
repeated Email emails = 3;
// optional, links
repeated Link links = 4;
// optional, birthday
string birthday = 5;
// optional, address
repeated Address addresses = 6;
// optional, social media
repeated SocialMedia social_medias = 7;
// optional, note of the contact
string note = 8;
}
message CreateResponse {
ContactInfo contact = 1;
}
message ReadRequest {
string id = 1;
}
message ReadResponse {
ContactInfo contact = 1;
}
message DeleteRequest {
// the id of the contact
string id = 1;
}
message DeleteResponse {
}
message UpdateRequest {
// required, the contact id
string id = 1;
// required, the name
string name = 2;
// optional, phone number
repeated Phone phones = 3;
// optional, emails
repeated Email emails = 4;
// optional, links
repeated Link links = 5;
// optional, birthday
string birthday = 6;
// optional, addresses
repeated Address addresses = 7;
// optional, social media
repeated SocialMedia social_medias = 8;
// optional, note
string note = 9;
}
message UpdateResponse {
ContactInfo contact = 1;
}
message ListRequest {
// optional
uint32 offset = 1;
// optional, default is 30
uint32 limit = 2;
}
message ListResponse {
repeated ContactInfo contacts = 1;
}