<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BetMania.Database;
using BetMania.Services.Models;
using System.Text;
using System.Web.Http.ValueProviders;
using BetMania.Services.CustomHeaderValueProviders;

namespace BetMania.Services.Controllers
{
    public class UserController : BaseApiController
    {
        // Constants
        private const int MinUsernameLength = 6;
        private const int MaxUsernameLength = 30;
        private const string ValidUsernameCharacters = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890_.";
        private const string ValidNicknameCharacters = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890_. -";
        private const string SessionKeyChars = "<KEY>";
        private const int SessionKeyLength = 50;
        private const int Sha1Length = 40;

        // Fields
        private BetManiaContext db;
        private readonly Random random;

        public UserController(BetManiaContext dbContext)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException("dbContext");
            }

            this.db = dbContext;
            this.random = new Random();
        }

        [HttpGet]
        [ActionName("getusers")]
        public HttpResponseMessage GetUsers([ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            this.ValidateSessionKey(sessionKey);

            var user = this.db.Users.FirstOrDefault(x => x.SessionKey == sessionKey);

            if (user == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
            }

            if (user.IsAdmin)
            {
                HttpResponseMessage response = this.ProcessOperation<HttpResponseMessage>(() =>
                {
                    var allUsers = this.db.Users;
                    return Request.CreateResponse(HttpStatusCode.OK, allUsers);
                });

                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User must be Administrator to access this!");
            }
        }

        [HttpPut]
        [ActionName("modify")]
        public HttpResponseMessage PutUser(User modifiedUser, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            this.ValidateSessionKey(sessionKey);

            var user = this.db.Users.FirstOrDefault(x => x.SessionKey == sessionKey);

            if (user == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
            }

            if (user.IsAdmin)
            {
                int id = user.Id;
                HttpResponseMessage response = this.ProcessOperation<HttpResponseMessage>(() =>
                {
                    var userToModify = this.db.Users.FirstOrDefault(x => x.Id == id);
                    userToModify = new User()
                    {
                        Id = modifiedUser.Id,
                        AuthCode = modifiedUser.AuthCode,
                        Avatar = modifiedUser.Avatar,
                        Balance = modifiedUser.Balance,
                        IsAdmin = modifiedUser.IsAdmin,
                        Nickname = modifiedUser.Nickname,
                        SessionKey = modifiedUser.SessionKey,
                        Username = modifiedUser.Username
                    };

                    db.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.NoContent);
                });
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.Forbidden);
            }

            return Request.CreateErrorResponse(HttpStatusCode.NotModified, "Modify failed!");
        }

        [HttpDelete]
        [ActionName("delete")]
        public HttpResponseMessage DeleteUser(int id, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            this.ValidateSessionKey(sessionKey);

            var user = this.db.Users.FirstOrDefault(x => x.SessionKey == sessionKey);

            if (user == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
            }

            if (user.IsAdmin)
            {
                HttpResponseMessage response = this.ProcessOperation<HttpResponseMessage>(() =>
                {
                    var userToDelete = this.db.Users.FirstOrDefault(x => x.Id == id);
                    if (userToDelete != null)
                    {
                        this.db.Users.Remove(userToDelete);
                    }

                    db.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.OK, userToDelete);
                });
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.Forbidden);
            }

            return Request.CreateErrorResponse(HttpStatusCode.NotModified, "Delete failed!");
        }

        // POST api/user/register
        [HttpPost]
        [ActionName("register")]
        public HttpResponseMessage Register(RegisterUserDTO userModel)<commit_msg>UserController modified - fixed modify and added get by id for users<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BetMania.Database;
using BetMania.Services.Models;
using System.Text;
using System.Web.Http.ValueProviders;
using BetMania.Services.CustomHeaderValueProviders;

namespace BetMania.Services.Controllers
{
    public class UserController : BaseApiController
    {
        // Constants
        private const int MinUsernameLength = 6;
        private const int MaxUsernameLength = 30;
        private const string ValidUsernameCharacters = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890_.";
        private const string ValidNicknameCharacters = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890_. -";
        private const string SessionKeyChars = "<KEY>";
        private const int SessionKeyLength = 50;
        private const int Sha1Length = 40;

        // Fields
        private BetManiaContext db;
        private readonly Random random;

        public UserController(BetManiaContext dbContext)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException("dbContext");
            }

            this.db = dbContext;
            this.random = new Random();
        }

        [HttpGet]
        [ActionName("getusers")]
        public HttpResponseMessage GetUsers([ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            this.ValidateSessionKey(sessionKey);

            var user = this.db.Users.FirstOrDefault(x => x.SessionKey == sessionKey);

            if (user == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
            }

            if (user.IsAdmin)
            {
                HttpResponseMessage response = this.ProcessOperation<HttpResponseMessage>(() =>
                {
                    var allUsers = this.db.Users;
                    return Request.CreateResponse(HttpStatusCode.OK, allUsers);
                });

                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User must be Administrator to access this!");
            }
        }

        [HttpGet]
        [ActionName("getuserbyid")]
        public HttpResponseMessage GetUsers(int id, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            HttpResponseMessage response = this.ProcessOperation<HttpResponseMessage>(() =>
            {
                this.ValidateSessionKey(sessionKey);

                var user = this.db.Users.FirstOrDefault(x => x.SessionKey == sessionKey);

                if (user == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
                }

                if (user.IsAdmin)
                {
                    var searchedUser = this.db.Users.FirstOrDefault(x => x.Id == id);

                    if (searchedUser == null)
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
                    }

                    return Request.CreateResponse(HttpStatusCode.Found);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.Forbidden);
                }
            });

            return response;
        }

        [HttpPut]
        [ActionName("modify")]
        public HttpResponseMessage PutUser(User modifiedUser, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            HttpResponseMessage response = this.ProcessOperation<HttpResponseMessage>(() =>
            {
                this.ValidateSessionKey(sessionKey);

                var user = this.db.Users.FirstOrDefault(x => x.SessionKey == sessionKey);

                if (user == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
                }

                if (user.IsAdmin)
                {
                    int id = user.Id;
                    var userToModify = this.db.Users.FirstOrDefault(x => x.Id == id);

                    if (userToModify == null)
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
                    }

                    userToModify.AuthCode = modifiedUser.AuthCode;
                    userToModify.Avatar = modifiedUser.Avatar;
                    userToModify.Balance = modifiedUser.Balance;
                    userToModify.IsAdmin = modifiedUser.IsAdmin;
                    userToModify.Nickname = modifiedUser.Nickname;
                    userToModify.SessionKey = modifiedUser.SessionKey;
                    userToModify.Username = modifiedUser.Username;

                    db.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.NoContent);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.Forbidden);
                }
            });

            return response;
        }

        [HttpDelete]
        [ActionName("delete")]
        public HttpResponseMessage DeleteUser(int id, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {

            HttpResponseMessage response = this.ProcessOperation<HttpResponseMessage>(() =>
            {
                this.ValidateSessionKey(sessionKey);

                var user = this.db.Users.FirstOrDefault(x => x.SessionKey == sessionKey);

                if (user == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
                }

                if (user.IsAdmin)
                {
                    var userToDelete = this.db.Users.FirstOrDefault(x => x.Id == id);
                    if (userToDelete == null)
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.NotFound, "User doesn't exist! Invalid session key!");
                    }

                    this.db.Users.Remove(userToDelete);
                    db.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.OK, userToDelete);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.Forbidden);
                }
            });

            return response;
        }

        // POST api/user/register
        [HttpPost]
        [ActionName("register")]
        public HttpResponseMessage Register(RegisterUserDTO userModel)
{'fraction_non_alphanumeric': 0.08334658819786862, 'fraction_numerical': 0.004453634483855575, 'mean_word_length': 1.7013963480128893, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6033624', 'n_tokens_mistral': 3132, 'n_tokens_neox': 2919, 'n_words': 593}