public async Task <IActionResult> CreateUserAccount([Bind("Id, username, password, email")] Account account, string userFirstNameParam = null)
        {
            if (ModelState.IsValid)
            {
                if (!UsernameExists(account.username))
                {
                    account.Id     = Guid.NewGuid();
                    account.isUser = true;
                    var sha256 = SHA256.Create();
                    var pass1  = sha256.ComputeHash(Encoding.UTF8.GetBytes(account.password));
                    var hash1  = BitConverter.ToString(pass1).Replace("-", "").ToLower();
                    account.password = hash1;
                    _context.Add(account);
                    User user = null;
                    if (userFirstNameParam == null)
                    {
                        string userId          = HttpContext.Session.GetString("registrationId");
                        string userFirstName   = HttpContext.Session.GetString("registrationFirstName");
                        string userLastName    = HttpContext.Session.GetString("registrationLastName");
                        string userGender      = HttpContext.Session.GetString("registrationGender");
                        string userDateOfBirth = HttpContext.Session.GetString("registrationBirthday");

                        user             = new User();
                        user.Id          = Guid.Parse(userId);
                        user.firstName   = userFirstName;
                        user.lastName    = userLastName;
                        user.gender      = Char.Parse(userGender);
                        user.dateOfBirth = DateTime.Parse(userDateOfBirth);

                        HttpContext.Session.Remove("registrationId");
                        HttpContext.Session.Remove("registrationFirstName");
                        HttpContext.Session.Remove("registrationLastName");
                        HttpContext.Session.Remove("registrationGender");
                        HttpContext.Session.Remove("registrationBirthday");
                    }
                    else
                    {
                        user = new User {
                            Id = new Guid(), firstName = "Test", lastName = "Unit", gender = 'F', dateOfBirth = new DateTime(1990, 3, 5)
                        };
                    }
                    user.UserAccount = account.Id;
                    _context.Add <User>(user);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    ModelState.AddModelError("", "Username already exists, please try again.");
                    return(View("~/Views/Accounts/CreateUser.cshtml"));
                }
            }
            return(View(account));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("Id,name,description,actionType,organizationId,creationDateTime,startDateTime,endDateTime")] CharityAction charityAction, string loggedOrg = null)
        {
            if (loggedOrg == null)
            {
                if (HttpContext.Session.GetString("username") == null)
                {
                    return(RedirectToAction("", ""));
                }
            }
            if (ModelState.IsValid)
            {
                string orgId = loggedOrg;
                if (orgId == null)
                {
                    orgId = HttpContext.Session.GetString("idOfLoggedAccount");
                }
                charityAction.organizationId   = Guid.Parse(orgId);
                charityAction.creationDateTime = DateTime.Now;

                charityAction.Id = Guid.NewGuid();
                _context.Add(charityAction);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(charityAction));
        }
        public bool DoPayment(Payment payment, CharityContext context)
        {
            // get object of user that is making the payment
            var userThatPays = context.user.Where(x => x.Id == payment.userSenderId).Single();
            // get object of organization that receives the payment
            var orgThatReceives = context.organization.Where(x => x.Id == payment.organizationReceiverId).Single();

            // get their credit card objects
            try
            {
                var creditCardThatPays     = context.card.Where(x => x.Id == userThatPays.creditCardId).Single();
                var creditCardThatReceives = context.card.Where(x => x.Id == orgThatReceives.creditCardNumber).Single();
                if (creditCardThatPays.amount >= payment.amount && payment.amount > 0) // payment is possible
                {
                    creditCardThatPays.amount     -= payment.amount;
                    creditCardThatReceives.amount += payment.amount;
                    context.Add(payment);
                    context.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #4
0
        public async Task <IActionResult> Create([Bind("Id,name,description,value,userDonatedId")] Item item, string loggedUser = null)
        {
            if (loggedUser == null)
            {
                if (HttpContext.Session.GetString("username") == null)
                {
                    return(RedirectToAction("", ""));
                }
            }
            if (ModelState.IsValid)
            {
                string userUsername = loggedUser;
                if (loggedUser == null)
                {
                    userUsername = HttpContext.Session.GetString("username");
                }

                var account = await _context.account.FirstOrDefaultAsync(m => m.username == userUsername);

                if (account == null)
                {
                    return(NotFound());
                }

                var user = await _context.user.FirstOrDefaultAsync(m => m.UserAccount == account.Id);

                if (user == null)
                {
                    return(NotFound());
                }
                item.Id            = Guid.NewGuid();
                item.userDonatedId = user.Id;
                _context.Add(item);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(item));
        }
예제 #5
0
 public async Task <bool> CreateAsync(Donation donation)
 {
     _context.Add(donation);
     return(await _context.SaveChangesAsync() > 0);
 }