Skip to content

Commit

Permalink
Merge pull request #32 from Dhruv9449/v2
Browse files Browse the repository at this point in the history
refactor: cli and web apps
  • Loading branch information
Dhruv9449 authored Feb 5, 2024
2 parents 62ceeda + 05bdca5 commit dc290ea
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 199 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
.db
data
credentials
test/
test/
backups/
prod/
.DS_Store
26 changes: 22 additions & 4 deletions vitty-backend-api/api/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,41 @@ import (
v1 "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/v1"
v2 "github.com/GDGVIT/vitty-backend/vitty-backend-api/api/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
)

func InitializeApi(f *fiber.App) {
func NewWebApi() *fiber.App {
// New fiber app
fiberApp := fiber.New()

fiberApp = fiber.New()
fiberApp.Use(logger.New())
fiberApp.Use(cors.New(
cors.Config{
AllowOrigins: "*",
AllowHeaders: "*",
AllowCredentials: true,
AllowMethods: "GET,POST,DELETE,PATCH,PUT,OPTIONS",
},
))

// Root endpoint
f.Get("/", func(c *fiber.Ctx) error {
fiberApp.Get("/", func(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).SendString("Welcome to VITTY API!🎉")
})

// Ping endpoint
f.Get("/ping", func(c *fiber.Ctx) error {
fiberApp.Get("/ping", func(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(
fiber.Map{
"detail": "pong",
})
})

api := f.Group("/api")
api := fiberApp.Group("/api")
v1.V1Handler(api)
v2.V2Handler(api)

return fiberApp
}
10 changes: 10 additions & 0 deletions vitty-backend-api/cli/commands/initialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package commands

import "github.com/urfave/cli/v2"

// AddCommands adds the commands to the app
func AddCommands(cliApp *cli.App) {
// Add the commands to the app
cliApp.Commands = append(cliApp.Commands, userCommands...)
cliApp.Commands = append(cliApp.Commands, TimetableCommands...)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package management
package commands

import (
"fmt"
Expand Down
149 changes: 149 additions & 0 deletions vitty-backend-api/cli/commands/userCommands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package commands

import (
"errors"
"fmt"

"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database"
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models"
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/utils"
"github.com/urfave/cli/v2"
)

var userCommands = []*cli.Command{
{
Name: "createsuperuser",
Aliases: []string{"csu"},
Usage: "Create a superuser",
Action: createSuperuser,
},
{
Name: "createadminuser",
Aliases: []string{"cau"},
Usage: "Create an admin user",
Action: createAdminuser,
},
{
Name: "getusers",
Aliases: []string{"gu"},
Usage: "Get users",
Action: getUsers,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "filter",
Aliases: []string{"f"},
Usage: "Filter users by",
Required: false,
},
&cli.StringFlag{
Name: "value",
Aliases: []string{"v"},
Usage: "Value of filter",
Required: false,
},
},
},
{
Name: "deleteuser",
Aliases: []string{"du"},
Usage: "Delete user",
Action: deleteUser,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
Aliases: []string{"k"},
Usage: "Key of user to delete",
Required: false,
},
&cli.StringFlag{
Name: "value",
Aliases: []string{"v"},
Usage: "Value of key",
Required: false,
},
},
},
}

func createSuperuser(c *cli.Context) error {
fmt.Println("Enter username: ")
var username string
fmt.Scanln(&username)

if !utils.CheckUserExists(username) {
return errors.New("user does not exist")
}
user := utils.GetUserByUsername(username)
user.Role = "superuser"
err := database.DB.Save(&user).Error
if err != nil {
return err
}
fmt.Println("Superuser created successfully")
return nil
}

func createAdminuser(c *cli.Context) error {
fmt.Println("Enter username: ")
var username string
fmt.Scanln(&username)

if !utils.CheckUserExists(username) {
return errors.New("user does not exist")
}
user := utils.GetUserByUsername(username)
user.Role = "admin"
err := database.DB.Save(&user).Error
if err != nil {
return err
}
fmt.Println("Admin user created successfully")
return nil
}

func getUsers(c *cli.Context) error {
// Take arguements as name of filter and value of filter
// If no arguements are given, return all users
// If arguements are given, return users that match the filter

filter := c.String("filter")
value := c.String("value")

var users []models.User
filter_query := fmt.Sprintf("%s = ?", filter)

if filter == "" || value == "" {
database.DB.Find(&users)
} else {
database.DB.Where(filter_query, value).Find(&users)
}

// Display Users in a table
fmt.Println("ID\tUsername\t\tRole\tRegNo\tEmail\tPicture")
for _, user := range users {
fmt.Printf("\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n", user.Username, user.Role, user.RegNo, user.Email, user.Picture)
}
return nil
}

func deleteUser(c *cli.Context) error {
key := c.String("key")
value := c.String("value")

if key == "" || value == "" {
fmt.Println("Enter key: ")
fmt.Scanln(&key)
fmt.Println("Enter value: ")
fmt.Scanln(&value)
}

var users models.User
filter_query := fmt.Sprintf("%s = ?", key)
err := database.DB.Where(filter_query, value).Delete(&users).Error
if err != nil {
fmt.Printf("Error: %s\n", err)
return err
}
fmt.Println("User deleted successfully")
return nil
}
28 changes: 28 additions & 0 deletions vitty-backend-api/cli/initialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vittyCli

import (
"github.com/GDGVIT/vitty-backend/vitty-backend-api/cli/commands"
"github.com/urfave/cli/v2"
)

func NewCliApp() *cli.App {
// New cli app
cliApp := cli.NewApp()

// Set the name, usage and version of the app
cliApp.Name = "Vitty"
cliApp.Usage = "Vitty Backend API"
cliApp.Version = "0.0.1"
cliApp.Authors = []*cli.Author{
{
Name: "Dhruv Shah",
Email: "[email protected]",
}}
cliApp.EnableBashCompletion = true
cliApp.Description = "CLI for Vitty Backend API"

// Set the commands
commands.AddCommands(cliApp)

return cliApp
}
30 changes: 0 additions & 30 deletions vitty-backend-api/cmd/management/createSuperuser.go

This file was deleted.

51 changes: 0 additions & 51 deletions vitty-backend-api/cmd/management/deleteUser.go

This file was deleted.

55 changes: 0 additions & 55 deletions vitty-backend-api/cmd/management/getUsers.go

This file was deleted.

Loading

0 comments on commit dc290ea

Please sign in to comment.