Commit e3b24fc1 authored by Azkee's avatar Azkee
Browse files

feat: create object representations for working with the sql db

parent adfff800
Loading
Loading
Loading
Loading

Models/Exercise.cs

0 → 100644
+19 −0
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;


namespace JuanLog.Models
{
    public partial class Exercise
    {
        [Key]
        public int ExerciseId { get; set; }
        public int CategoryId { get; set; }
        public required string Name { get; set; }

    }
}
+5 −24
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace JuanLog.Models
{
    public static class ExerciseCategory
    public partial class ExerciseCategory
    {
        public static HashSet<string> ExerciseCategories = new()
        {
            "Arm strength",
            "Back exercise",
            "Upper leg exercise",
            "Stretching",
            "Funny quirky movement",
        };

        public static bool addCategory(string newCategory)
        {
            return ExerciseCategories.Add(newCategory);
        }

        public static bool removeCategory(string newCategory)
        {
            return ExerciseCategories.Remove(newCategory);
        }
        [Key]
        public int CategoryId { get; set; }
        public required string Name { get; set; }
    }
}
+10 −20
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;

namespace JuanLog.Models
{
    public class ExerciseEntry
    public partial class ExerciseEntry
    {
        private string connectionString = @"server=(localdb)\MSSQLLocalDB; Initial Catalog=JuanLogDB; Integrated Security=true";
        private DateTime _date;
        private string _category;
        private int _weight;
        private int _reps;
        private int _sets;
        public ExerciseEntry(DateTime date, string category, int weight, int reps, int sets)
        {
            if (!ExerciseCategory.ExerciseCategories.Contains(category))
            {
                throw new ArgumentException("Tato kategorie neexistuje, Juane, budeš ji muset vytvořit.");
            }

            _date = date;
            _category = category;
            _weight = weight;
            _reps = reps;
            _sets = sets;
        }
        [Key]
        public int EntryId { get; set; }
        public int UserId { get; set; }
        public int ExerciseId { get; set; }
        public DateTime When { get; set; }
        public int Weight { get; set; }
        public int RepKey { get; set; }
    }
}
+5 −48
Original line number Diff line number Diff line
@@ -14,6 +14,10 @@ namespace JuanLog.Models
        private string connectionString = @"server=(localdb)\MSSQLLocalDB; Initial Catalog=JuanLogDB; Integrated Security=true";

        public DbSet<User> Users { get; set; }
        public DbSet<ExerciseEntry> ExerciseEntries { get; set; }
        public DbSet<Exercise> Exercises { get; set; }
        public DbSet<ExerciseCategory> ExerciseCategories { get; set; }
        public DbSet<Set> SetTable { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
@@ -22,53 +26,6 @@ namespace JuanLog.Models
            Debug.WriteLine("Anyway, connection established");
        }

        public User? CheckUserPassword(string user, string password)
        {
            Debug.WriteLine("Entered pwd checker");
            using var db = new JuanLogDBContext();

            Debug.WriteLine(user);
            Debug.WriteLine(user == null);
            List<User> matchingUsers = db.Users.Where(u => u.Name == user).ToList();
            
            if (matchingUsers.Count == 0) { Debug.WriteLine("Ended with no user ://"); return null; }

            User activeUser = matchingUsers.First();
            Debug.WriteLine("eee... got the user");

            string actualHashedPassword = activeUser.HashedPassword;
            Debug.WriteLine("Received hashed pwd");

            byte[] hashBytes = Convert.FromBase64String(actualHashedPassword);
            
            // RETRIEVE SALT
            byte[] salt = new byte[16];
            Array.Copy(hashBytes, 0, salt, 0, 16);
            Debug.WriteLine("Got salt");
        
            // HASH THE CLAIMED PASSWORD WITH SALT
            var hashMaker = new Rfc2898DeriveBytes(password, salt, 100000, HashAlgorithmName.SHA256);
            byte[] hash = hashMaker.GetBytes(16);

            Debug.WriteLine("compare hash made");
            byte[] combinedBytes = new byte[32];
            Array.Copy(salt, 0, combinedBytes, 0, 16);
            Array.Copy(hash, 0, combinedBytes, 16, 16);
            string hashedPassword = Convert.ToBase64String(combinedBytes);

            Debug.WriteLine("Magiccc!");
            // COMPARE HASHES
            if (hashedPassword != actualHashedPassword)
            {
                Debug.WriteLine("Wrong password");
                Debug.WriteLine(hashedPassword);
                Debug.WriteLine(actualHashedPassword);
                return null;
            }

            Debug.WriteLine("Returned from here with user");
            return activeUser;

        }
    }
}

Models/Set.cs

0 → 100644
+14 −0
Original line number Diff line number Diff line
using System.ComponentModel.DataAnnotations;


namespace JuanLog.Models
{
    public partial class Set
    {
        [Key]
        public int SetId { get; set; }
        public int EntryId { get; set; }
        public int Repetitions { get; set; }

    }
}
Loading