mirror of
https://github.com/netscrawler/changeAPI.git
synced 2026-09-05 08:29:36 +00:00
Squashed commit of the following:
commit5b11756af6Author: netscrawler <mariarthyjamesoo@gmail.com> Date: Thu Jul 4 00:44:15 2024 +0300 v0.1 commitc99f477e35Author: netscrawler <mariarthyjamesoo@gmail.com> Date: Sun Jun 30 20:44:53 2024 +0300 v0.0.6
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package cnvrt
|
||||
|
||||
import (
|
||||
"context"
|
||||
cnvrtv1 "github.com/netscrawler/protoss/gen/go/changeAPI"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type Converter interface {
|
||||
Convert(ctx context.Context,
|
||||
amount uint32,
|
||||
targetCurrency string,
|
||||
) (convertedAmount uint32, rate float32, err error)
|
||||
}
|
||||
|
||||
type serverAPI struct {
|
||||
cnvrtv1.UnimplementedConverterServer
|
||||
convert Converter
|
||||
}
|
||||
|
||||
func Register(gRPC *grpc.Server, convert Converter) {
|
||||
cnvrtv1.RegisterConverterServer(gRPC, &serverAPI{convert: convert})
|
||||
}
|
||||
|
||||
func (s *serverAPI) Convert(
|
||||
ctx context.Context,
|
||||
req *cnvrtv1.ConvertRequest) (
|
||||
*cnvrtv1.ConvertResponse, error) {
|
||||
if !isValidCurrency(req.GetTargetCurrency()) {
|
||||
return nil, status.Error(codes.InvalidArgument, "Invalid target currency")
|
||||
}
|
||||
|
||||
convertedAmount, rate, err := s.convert.Convert(ctx, req.GetAmount(), req.GetTargetCurrency())
|
||||
if err != nil {
|
||||
//todo error handler
|
||||
return nil, status.Error(codes.Internal, "Internal error")
|
||||
}
|
||||
return &cnvrtv1.ConvertResponse{
|
||||
BaseAmount: req.GetAmount(),
|
||||
ConvertedAmount: convertedAmount,
|
||||
ConvertedCurrency: req.GetTargetCurrency(),
|
||||
Rate: rate,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func isValidCurrency(currency string) bool {
|
||||
|
||||
currencies := map[string]bool{
|
||||
"AUD": true, "GBP": true, "BYR": true, "DKK": true, "USD": true, "EUR": true,
|
||||
"ISK": true, "KZT": true, "CAD": true, "NOK": true, "XDR": true, "SGD": true,
|
||||
"TRL": true, "UAH": true, "SEK": true, "CHF": true, "JPY": true,
|
||||
}
|
||||
if currencies[currency] {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user