using System.Data;
using AfRApay.Web.Backend;
using LinqToDB;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace AfRApay.Web.Pages;

public class UpdateBalanceModel : PageModel {
	public void OnGet(int id, decimal amount) {
		var db = new Database.DbConn();
		var user = db.Users.FirstOrDefault(p => p.Id == id);
		if (user == null) {
			Response.Redirect("/Error");
			return;
		}

		switch (user.Balance + amount) {
			case < -50: throw new ConstraintException("Balance too low!");
			case > 999: throw new ConstraintException("Balance too high!");
		}

		user.Balance += amount;
		db.Update(user);
		Response.Redirect($"/#{user.Nickname}");
	}
}
