Esempio n. 1
0
        public async Task <ActionResult <APIResponse <LoginAPIModel.Response> > > Login(LoginAPIModel.Request requestModel)
        {
            // Defining empty response
            var responseModel = new APIResponse <LoginAPIModel.Response>();

            // Defining a user
            ApplicationUser user;

            // Check if the passed parameter is an email or a user name
            if (requestModel.UserNameOrEmail.Contains('@'))
            {
                // if email, search for the user using the email
                user = await _userManager.FindByEmailAsync(requestModel.UserNameOrEmail);
            }
            else
            {
                // if username, search for the user using userName
                user = await _userManager.FindByNameAsync(requestModel.UserNameOrEmail);
            }

            // If no user found
            if (user == null)
            {
                // Assigning the error and return
                responseModel.AddError("كلمة المرور لا تطابق الحساب");
                return(responseModel);
            }

            // if there is a user, attempt to sign in
            var result = await _signInManager.PasswordSignInAsync(user, requestModel.Passward, false, false);

            // If the sign in was successfull
            if (result.Succeeded)
            {
                // Return the response with the generated token
                responseModel.Response = new LoginAPIModel.Response()
                {
                    Token  = await GenerateJSONWebToken(user),
                    UserId = user.Id
                };

                return(Ok(responseModel));
            }

            // If the attempt was a failure, return an error
            responseModel.AddError("كلمة المرور لا تطابق الحساب");

            return(responseModel);
        }
        public async Task <ActionResult <APIResponse> > UpdateChore(UpdateChoreAPIModel.Request requestModel)
        {
            // Get the current logged in user
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            var responseModel = new APIResponse <UpdateRoomAPIModel.Response>();

            var room = await _context.Rooms
                       // Include the room users (join)
                       .Include(room => room.RoomUsers)
                       // Include the user of room user (join)
                       .ThenInclude(roomUser => roomUser.User)
                       // Select the required room and make sure that this user is a member of it
                       .FirstOrDefaultAsync(room => room.Id == requestModel.RoomId &&
                                            room.RoomUsers.Select(roomUser => roomUser.User.Id).Contains(user.Id));

            // Checck if the user is a member of this room and the room exist
            if (room == null)
            {
                responseModel.AddError("رقم الغرفة غير صحيح");

                // Return the response
                return(responseModel);
            }
            // Get the chore we are trying to update
            var chore = await _context.ToDoItems
                        .Include(toDoItem => toDoItem.Room)
                        .FirstOrDefaultAsync(toDoItem => toDoItem.Id == requestModel.ChoreId && toDoItem.Room.Id == requestModel.RoomId);

            // Checck if the user is a member of this room and the room exist
            if (chore == null)
            {
                responseModel.AddError("رقم الواجب غير صحيح");

                // Return the response
                return(responseModel);
            }
            // Update the chore
            chore.Done      = true;
            chore.DoingTime = DateTime.UtcNow;
            chore.Doer      = user;

            // Save the changes
            await _context.SaveChangesAsync();

            return(Ok(responseModel));
        }
        public async Task <ActionResult <APIResponse <CreateChoreAPIModel.Response> > > CreateChore(CreateChoreAPIModel.Request requestModel)
        {
            // Get the current logged in user
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            // Initiate the response model
            var responseModel = new APIResponse <CreateChoreAPIModel.Response>();

            var room = await _context.Rooms
                       .Include(room => room.RoomUsers)
                       .ThenInclude(roomUser => roomUser.User)
                       .FirstOrDefaultAsync(room =>
                                            room.Id == requestModel.RoomId &&
                                            room.RoomUsers.Select(roomUser => roomUser.User.Id).Contains(user.Id) &&
                                            (room.AllowMembersToPost || room.RoomUsers.FirstOrDefault(roomUser => roomUser.User.Id == user.Id).Owner)
                                            );

            if (room == null)
            {
                responseModel.AddError("ليس لديك الصلاحية للنشر");

                return(responseModel);
            }

            var chore = new ToDoItem()
            {
                Description = requestModel.Description,
                Room        = room,
                Urgency     = requestModel.Urgency
            };

            await _context.ToDoItems.AddAsync(chore);

            await _context.SaveChangesAsync();

            responseModel.Response = new CreateChoreAPIModel.Response()
            {
                ChoreId     = chore.Id,
                Description = chore.Description,
                Urgency     = chore.Urgency
            };

            // Return the response
            return(Ok(responseModel));
        }