Example #1
0
        //[BindProperty]
        //public User createUser { get; set; }

        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public JsonResult OnPost()
        {
            string username     = Request.Form["username"].First();
            string passwordHash = Request.Form["passwordHash"].First();
            string salt         = Request.Form["salt"].First();
            string group_name   = Request.Form["group_name"];
            string is_observer  = Request.Form["is_observer"];

            var    userObjArray = _context.User.Where(x => x.UserName == username);
            string error        = "";

            var   any_group = _context.Group.Where(x => x.Name == group_name);
            Group group;

            if (any_group.Count() == 0 || !any_group.Any())
            {
                group = new Group
                {
                    Name = group_name,
                };
            }
            else
            {
                group = _context.Group.First(x => x.Name == group_name);
            }

            if (userObjArray.Any())
            {
                error = "Username already exists.";
            }
            else
            {
                //if (group == null)
                //{
                //    group = new Group
                //    {
                //        Name = group_name,
                //    };
                //}

                User createdUser = new User
                {
                    UserName     = username,
                    PasswordHash = passwordHash,
                    Salt         = salt,
                    IsObserver   = is_observer,
                    GroupID      = group.ID,
                    Group        = group
                };
                _context.User.Add(createdUser);
                _context.SaveChanges();
            }

            var returnableUser = new ReturnableUser("", "", "", "", error);

            return(new JsonResult(returnableUser));
        }
Example #2
0
        /// <summary>
        /// this function is called to save time entries to the database
        /// </summary>
        public JsonResult OnPostSubmitTime()
        {
            DateTime tempStartTime = new DateTime();
            DateTime tempEndTime   = new DateTime();
            string   error         = "";

            try
            {
                tempStartTime = DateTime.Parse(Request.Form["StartTime"].First());
                tempEndTime   = DateTime.Parse(Request.Form["EndTime"].First());
            }
            catch (FormatException)
            {
                Console.WriteLine("Unable to parse the specified date");
                error = "Unable to parse the specified date";
            }


            int index = 0;

            for (int j = 0; j < _context.User.Count(); j++)
            {
                if (_context.User.ToArray()[j].UserName == Request.Form["name"].First())
                {
                    index = j;
                    break;
                }
            }


            TimeLog newTimeEntry = new TimeLog
            {
                StarTime    = tempStartTime,
                EndTime     = tempEndTime,
                UserID      = _context.User.ToArray()[index].ID,
                User        = _context.User.ToArray()[index],
                Description = Request.Form["Description"].First()
            };

            _context.TimeLog.Add(newTimeEntry);
            _context.SaveChanges();

            var returnableUser = new ReturnableUser("", "", "", "", error);

            return(new JsonResult(returnableUser));
        }