Skip to content

Commit

Permalink
more simplification work
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 22, 2023
1 parent 23be7be commit c03dbc8
Show file tree
Hide file tree
Showing 7 changed files with 676 additions and 686 deletions.
1,290 changes: 644 additions & 646 deletions server/api/tumdev/campus_backend.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/api/tumdev/campus_backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ enum DeviceType {

message CreateDeviceRequest {
string device_id = 1;
optional string public_key = 2;
string public_key = 2;
DeviceType device_type = 3;
}

Expand Down
14 changes: 7 additions & 7 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func queryTags(cafeteriaID int32, dishID int32, ratingType ModelType, tx *gorm.D
}

if err != nil {
log.WithError(err).Error("while querying the tags for the request.")
log.WithError(err).Error("while querying the tags for the request")
}

//needed since the gRPC element does not specify column names - cannot be directly queried into the grpc message object.
Expand Down Expand Up @@ -672,25 +672,25 @@ func (s *CampusServer) GetCafeterias(ctx context.Context, _ *pb.ListCanteensRequ
}, requestStatus
}

func (s *CampusServer) ListDishes(ctx context.Context, request *pb.ListDishesRequest) (*pb.ListDishesReply, error) {
if request.Year < 2022 {
func (s *CampusServer) ListDishes(ctx context.Context, req *pb.ListDishesRequest) (*pb.ListDishesReply, error) {
if req.Year < 2022 {
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Years must be larger or equal to 2022 ") // currently, no previous values have been added
}
if request.Week < 1 || request.Week > 53 {
if req.Week < 1 || req.Week > 53 {
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Weeks must be in the range 1 - 53")
}
if request.Day < 0 || request.Day > 4 {
if req.Day < 0 || req.Day > 4 {
return &pb.ListDishesReply{}, status.Error(codes.Internal, "Days must be in the range 1 (Monday) - 4 (Friday)")
}

var requestStatus error = nil
var results []string
err := s.db.WithContext(ctx).Table("dishes_of_the_week weekly").
Where("weekly.day = ? AND weekly.week = ? and weekly.year = ?", request.Day, request.Week, request.Year).
Where("weekly.day = ? AND weekly.week = ? and weekly.year = ?", req.Day, req.Week, req.Year).
Select("weekly.dishID").
Joins("JOIN dish d ON d.dish = weekly.dishID").
Joins("JOIN cafeteria c ON c.cafeteria = d.cafeteriaID").
Where("c.name LIKE ?", request.CanteenId).
Where("c.name LIKE ?", req.CanteenId).
Select("d.name").
Find(&results).Error

Expand Down
4 changes: 2 additions & 2 deletions server/backend/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *CampusServer) CreateDevice(_ context.Context, req *pb.CreateDeviceReque
return nil, status.Error(codes.InvalidArgument, err.Error())
}

switch req.GetDeviceType() {
switch req.DeviceType {
case pb.DeviceType_ANDROID:
return nil, status.Error(codes.Unimplemented, "android device creation not implemented")
case pb.DeviceType_IOS:
Expand All @@ -146,7 +146,7 @@ func (s *CampusServer) DeleteDevice(_ context.Context, req *pb.DeleteDeviceReque
return nil, status.Error(codes.InvalidArgument, err.Error())
}

switch req.GetDeviceType() {
switch req.DeviceType {
case pb.DeviceType_ANDROID:
return nil, status.Error(codes.Unimplemented, "android device remove not implemented")
case pb.DeviceType_IOS:
Expand Down
12 changes: 6 additions & 6 deletions server/backend/ios_notifications/device/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var (
})
)

func (service *Service) CreateDevice(request *pb.CreateDeviceRequest) (*pb.CreateDeviceReply, error) {
func (service *Service) CreateDevice(req *pb.CreateDeviceRequest) (*pb.CreateDeviceReply, error) {
device := model.IOSDevice{
DeviceID: request.GetDeviceId(),
PublicKey: request.GetPublicKey(),
DeviceID: req.DeviceId,
PublicKey: req.PublicKey,
}

if err := service.Repository.CreateDevice(&device); err != nil {
Expand All @@ -39,14 +39,14 @@ func (service *Service) CreateDevice(request *pb.CreateDeviceRequest) (*pb.Creat
}, nil
}

func (service *Service) DeleteDevice(request *pb.DeleteDeviceRequest) (*pb.DeleteDeviceReply, error) {
if err := service.Repository.DeleteDevice(request.GetDeviceId()); err != nil {
func (service *Service) DeleteDevice(req *pb.DeleteDeviceRequest) (*pb.DeleteDeviceReply, error) {
if err := service.Repository.DeleteDevice(req.DeviceId); err != nil {
return nil, status.Error(codes.Internal, "Could not delete device")
}

iosRegisteredDevices.Dec()
return &pb.DeleteDeviceReply{
DeviceId: request.GetDeviceId(),
DeviceId: req.DeviceId,
}, nil
}

Expand Down
30 changes: 11 additions & 19 deletions server/backend/ios_notifications/request_response/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,26 @@ var collectedNewGrades = promauto.NewHistogram(prometheus.HistogramOpts{
Buckets: prometheus.LinearBuckets(0, 5, 5),
})

func (service *Service) HandleDeviceRequestResponse(request *pb.IOSDeviceRequestResponseRequest, apnsIsActive bool) (*pb.IOSDeviceRequestResponseReply, error) {
// requestId refers to the request id that was sent to the device and stored in the Database
requestId := request.GetRequestId()
func (service *Service) HandleDeviceRequestResponse(req *pb.IOSDeviceRequestResponseRequest, apnsIsActive bool) (*pb.IOSDeviceRequestResponseReply, error) {
log.WithField("requestId", req.RequestId).Trace("Handling request")

log.WithField("requestId", requestId).Trace("Handling request")

requestLog, err := service.Repository.GetIOSDeviceRequest(requestId)
requestLog, err := service.Repository.GetIOSDeviceRequest(req.RequestId)
if err != nil {
log.WithError(err).Error("Could not get request")
return nil, status.Error(codes.Internal, "Could not get request, probably request is already outdated")
}

switch requestLog.RequestType {
case model.IOSBackgroundCampusTokenRequest.String():
campusToken := request.GetPayload()

if campusToken == "" {
if req.Payload == "" {
return nil, status.Error(codes.InvalidArgument, "Payload is empty")
}

if !apnsIsActive {
return nil, status.Error(codes.Internal, "APNS is not active")
}

return service.handleDeviceCampusTokenRequest(requestLog, campusToken)
return service.handleDeviceCampusTokenRequest(requestLog, req.Payload)
default:
return nil, status.Error(codes.InvalidArgument, "Unknown request type")
}
Expand Down Expand Up @@ -92,7 +87,9 @@ func (service *Service) handleDeviceCampusTokenRequest(requestLog *model.IOSDevi
collectedNewGrades.Observe(float64(len(newGrades)))
if len(newGrades) == 0 {
log.Info("No new grades found")
service.deleteRequestLog(requestLog)
if err := service.Repository.DeleteAllRequestLogsForThisDeviceWithType(requestLog); err != nil {
log.WithError(err).Error("Could not delete request logs")
}
return &pb.IOSDeviceRequestResponseReply{
Message: "Successfully handled request",
}, nil
Expand All @@ -113,20 +110,15 @@ func (service *Service) handleDeviceCampusTokenRequest(requestLog *model.IOSDevi
sendGradesToDevice(device, newGrades, apnsRepository)
}

service.deleteRequestLog(requestLog)
if err := service.Repository.DeleteAllRequestLogsForThisDeviceWithType(requestLog); err != nil {
log.WithError(err).Error("Could not delete request logs")
}

return &pb.IOSDeviceRequestResponseReply{
Message: "Successfully handled request",
}, nil
}

func (service *Service) deleteRequestLog(requestLog *model.IOSDeviceRequestLog) {
err := service.Repository.DeleteAllRequestLogsForThisDeviceWithType(requestLog)
if err != nil {
log.WithError(err).Error("Could not delete request logs")
}
}

func decryptGrades(grades []model.IOSEncryptedGrade, campusToken string) ([]model.IOSEncryptedGrade, error) {
oldGrades := make([]model.IOSEncryptedGrade, len(grades))
for i, encryptedGrade := range grades {
Expand Down
10 changes: 5 additions & 5 deletions server/backend/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
)

func ValidateCreateDevice(request *pb.CreateDeviceRequest) error {
if request.GetDeviceId() == "" {
func ValidateCreateDevice(req *pb.CreateDeviceRequest) error {
if req.DeviceId == "" {
return errors.New("deviceId is empty")
}

if request.GetDeviceType() == pb.DeviceType_IOS && request.GetPublicKey() == "" {
if req.DeviceType == pb.DeviceType_IOS && req.PublicKey == "" {
return errors.New("publicKey is needed for IOS")
}

return nil
}

func ValidateDeleteDevice(request *pb.DeleteDeviceRequest) error {
if request.GetDeviceId() == "" {
func ValidateDeleteDevice(req *pb.DeleteDeviceRequest) error {
if req.DeviceId == "" {
return errors.New("deviceId is empty")
}

Expand Down

0 comments on commit c03dbc8

Please sign in to comment.