public ActionResult <IEnumerable <UserIdDTO> > Put([FromBody] UserIdDTO user)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Model State is invalid");
                }

                var updateProccess = _app.Update(user);

                if (updateProccess.Succeeded == true)
                {
                    return(Ok("User updated successfully"));
                }
                else
                {
                    throw new Exception("falha ao atualizar o usuário");
                }
            }
            catch (Exception ex)
            {
                _err.Add(ex, HttpContext.User.Identity.Name);
                return(BadRequest(ex.Message));
            }
        }
Пример #2
0
        public IdentityResult Update(UserIdDTO user)
        {
            var mappedUser     = _mapper.Map <IdentityUser>(user);
            var updateProccess = _repo.Update(mappedUser);

            return(updateProccess);
        }
        public async Task <ActionResult> PostNewCart([FromBody] UserIdDTO u)
        {
            //TODO: Seperation of concern
            //TODO: Exception handling
            Order o = new Order();
            GenericResponseDTO genericResponseDTO = new GenericResponseDTO();

            //Check if user exists
            if (u.UserId != 0)
            {
                try
                {
                    await _context.users.FirstAsync(x => x.UserId == u.UserId);
                }
                catch
                {
                    genericResponseDTO.Message = "No user with that ID found";
                    return(NotFound(genericResponseDTO));
                }

                o.UserId = u.UserId;
            }
            else
            {
                o.UserId = 1;
            }

            _context.orders.Add(o);
            await _context.SaveChangesAsync();

            // Generate an idempotency token to ensure our requests are only handled once, in case of connection issues, etc.
            o.IdempotencyToken = Guid.NewGuid().ToString();

            Random rand = new Random();

            int itemAmount = rand.Next(5);

            List <Item> items = await _context.items.Where(x => x.Active == true).ToListAsync();

            List <Item> cart = new List <Item>();

            for (int i = 0; i < itemAmount; i++)
            {
                Random rdm   = new Random();
                int    index = rdm.Next(items.Count());
                cart.Add(items[index]);
                OrderItem tempOrderItem = new OrderItem(o.OrderId, items[index].ItemId);
                _context.orderItems.Add(tempOrderItem);
            }

            await _context.SaveChangesAsync();

            CartDTO cartToReturn = new CartDTO(o.OrderId, cart);

            return(Ok(cartToReturn));
        }
Пример #4
0
        public async Task <IActionResult> GetUsername([FromBody] UserIdDTO model)
        {
            var user = await _userManager.FindByIdAsync(model.UserId);

            if (user == null)
            {
                return(RequestHandler.UserNotFound());
            }

            return(Ok(new { username = user.UserName }));
        }
        public void TestGetUsersIdsByUsernames()
        {
            UserIdDTO userIds = new UserIdDTO();

            userIds.UserID = new Dictionary <string, string>();
            userIds.UserID.Add("TestUser1", "0");
            userIds.UserID.Add("TestUser2", "0");
            userIds.UserID.Add("Non-existing user", "0");

            // The ID of the currently authorised user can be found and modified in Helper/HttpConextHelper
            userIds = _userController.GetUsersIdsByUsernames(userIds).Result;
            Assert.AreEqual("998", userIds.UserID["TestUser1"]);
            Assert.AreEqual("999", userIds.UserID["TestUser2"]);
            Assert.AreEqual("0", userIds.UserID["Non-existing user"]);
        }
Пример #6
0
        public UserIdDTO FindByEmail(string email)
        {
            IdentityUser user = _repo.FindByEmail(email).Result;

            if (user != null)
            {
                UserIdDTO userWithId = new UserIdDTO
                {
                    Id       = user.Id,
                    UserName = user.UserName,
                    Email    = user.Email
                };

                return(userWithId);
            }

            return(null);
        }
        public async Task <UserIdDTO> GetUsersIdsByUsernames(UserIdDTO userIds)
        {
            // Search for nicknames in the database, assign the found id to as a value to the dictionary
            UserIdDTO usernameIds = userIds;

            usernameIds.UserID = userIds.UserID;
            foreach (String entry in userIds.UserID.Keys.ToList())
            {
                if (_database.User.FirstOrDefault(u => u.UserName.ToLower() == entry.ToLower()) != null)
                {
                    string userId = _database.User.FirstOrDefault(u => u.UserName.ToLower().Equals(entry.ToLower())).Id.ToString();
                    usernameIds.UserID[entry] = userId;
                }
                else
                {
                    usernameIds.UserID[entry] = "0";
                }
            }
            return(usernameIds);
        }
Пример #8
0
        public async Task <float> getUserMoneyAsync(UserIdDTO data)
        {
            var allExpenses = await GetExpenses(new GetExpensesDTO
            {
                ownerId = data.userId,
                maxNumberOfExpensesToShow = -1,
                numberOfDaysToShow        = -1
            });

            var allIncomes = await inc.GetAllIncomes(new GetAllDTO
            {
                ownerId = data.userId,
                maxNumberOfIncomesToShow = -1,
                numberOfDaysToShow       = -1
            });

            var expensesSum = allExpenses.Sum(x => x.Moneyused);
            var incomesSum  = allIncomes.Sum(x => x.Moneyrecieved);

            return(incomesSum - expensesSum);
        }
Пример #9
0
        public IActionResult NewGame([FromBody] UserIdDTO id)
        {
            var  userId       = _signInManager.UserManager.GetUserId(User);
            var  opponentId   = id.UserID;
            bool tooManyGames = _GameRepo.GetAllGames(userId).Where(p => p.OpponentName.Equals(_GameRepo.GetUserNameFromId(opponentId))).Count() >= 3;

            if (tooManyGames)
            {
                return(Json(new GameRefreshData {
                    GameID = ""
                }));
            }

            var newGameID = _GameRepo.AddNewGame(_signInManager.UserManager.GetUserId(User), id.UserID);

            var data = new GameRefreshData {
                GameID = newGameID
            };

            return(Json(data));
        }
Пример #10
0
 public async Task <Users> GetUserById(UserIdDTO data)
 {
     return(await Task.Run(() => context.Users.Find(data.userId)));
 }