Skip to content

Commit

Permalink
Merge pull request #16 from alik-r/no_deposits
Browse files Browse the repository at this point in the history
Remove deposit functionality from user API
  • Loading branch information
alik-r authored Nov 20, 2024
2 parents 4f2ab7d + f91cf5d commit 1cb7bcb
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 83 deletions.
1 change: 0 additions & 1 deletion backend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func main() {
r.Group(func(r chi.Router) {
r.Use(middleware.JWTAuth)
r.Get("/api/user", api.GetUser)
r.Post("/api/user/deposit", api.Deposit)
r.Get("/api/leaderboard", api.GetLeaderboard)
r.Post("/api/roulette", api.PlaceBet)
})
Expand Down
46 changes: 0 additions & 46 deletions backend/pkg/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ type RegisterRequest struct {
Avatar string `json:"avatar,omitempty"`
}

type DepositRequest struct {
Amount int `json:"amount"`
}

type BetRequest struct {
BetAmount int `json:"bet_amount"`
BetType string `json:"bet_type"`
Expand Down Expand Up @@ -146,48 +142,6 @@ func Register(w http.ResponseWriter, r *http.Request) {
})
}

func Deposit(w http.ResponseWriter, r *http.Request) {
username, ok := r.Context().Value(middleware.UsernameKey).(string)
if !ok || username == "" {
http.Error(w, "Unauthenticated user", http.StatusUnauthorized)
return
}

var deposit DepositRequest
err := json.NewDecoder(r.Body).Decode(&deposit)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if deposit.Amount <= 0 {
http.Error(w, "Invalid deposit amount", http.StatusBadRequest)
return
}

var user models.User
err = db.DB.Where("username = ?", username).First(&user).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
http.Error(w, "User not found", http.StatusNotFound)
return
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
user.Balance += deposit.Amount
if err := db.DB.Save(&user).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(user)
}

func PlaceBet(w http.ResponseWriter, r *http.Request) {
username, ok := r.Context().Value(middleware.UsernameKey).(string)
if !ok || username == "" {
Expand Down
35 changes: 2 additions & 33 deletions frontend/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
<div class="avatar">
<img :src="user.avatar" alt="User Avatar">
</div>
<p class="big-username" x-text="user.username">Username</p>
<p class="user-text" x-text="user.username">Username</p>
<p class="user-text" x-text="user.email">Email</p>
</div>
<div class="profile-right">
<div class="souls">
Expand All @@ -63,12 +64,6 @@
<img src="images/soul_icon.png" alt="Soul Icon" class="soul-icon">
</div>
</div>
<form class="deposit-form" @submit.prevent="deposit">
<label for="deposit-amount">Deposit Amount</label>
<input type="number" id="deposit-amount" x-model="depositAmount" placeholder="Enter amount"
required>
<button type="submit" class="deposit-button">Deposit</button>
</form>
</div>
</div>
</main>
Expand Down Expand Up @@ -99,32 +94,6 @@
})
.then(data => this.user = data);
},
deposit() {
const depositAmount = parseInt(this.depositAmount);
fetch('/api/user/deposit', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.authToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ amount: depositAmount })
})
.then(response => {
if (response.status === 401) {
alert('Invalid auth token. Please log in again.');
window.location.href = '/register';
}
return response.json();
})
.then(data => {
if (data.balance) {
this.user.balance = data.balance;
} else {
alert('Deposit failed!');
console.error(data);
}
});
},
logout() {
localStorage.removeItem('authToken');
window.location.href = '/register.html';
Expand Down
5 changes: 2 additions & 3 deletions frontend/styles/profile.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
justify-content: space-between;
align-items: center;
gap: 40px;
width: 80%;
margin-top: 20px;
padding: 20px;
background-color: #FFF0B3;
background-color: #ffda46;
border-radius: 12px;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
}
Expand All @@ -36,7 +35,7 @@
border: 4px solid #3F1B12;
}

.big-username {
.user-text {
font-size: 20px;
font-weight: bold;
color: #3F1B12;
Expand Down

0 comments on commit 1cb7bcb

Please sign in to comment.