Skip to content

Commit

Permalink
Merge pull request #77 from jonaspaq/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
jonaspaq authored Apr 14, 2020
2 parents e967e3e + 292ad34 commit 3b7ca2b
Show file tree
Hide file tree
Showing 22 changed files with 704 additions and 561 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Veza
name: Feature Tests

on: [push]

Expand Down
62 changes: 52 additions & 10 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Veza
name: Node

on: [push]

jobs:
lint-styles:
lint-scss:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 13.x]
node-version: [12.x]

steps:
- uses: actions/checkout@v2
Expand All @@ -18,24 +18,66 @@ jobs:
with:
node-version: ${{ matrix.node-version }}

- name: Cache node modules
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Cache dependencies
uses: actions/cache@v1
id: yarn-cache
env:
cache-name: cache-node-modules
cache-name: cache-dependencies
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- run: yarn install
- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

- name: Lint scss
run: npm run lint

- name: Test build for production
run: npm run production
build-vue-files:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]

steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Cache dependencies
uses: actions/cache@v1
id: yarn-cache
env:
CI: true
cache-name: cache-dependencies
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

- name: Test build for production
run: yarn production
96 changes: 96 additions & 0 deletions app/Http/Controllers/Api/MessageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\SendMessage;
use App\MessageThread;
use App\Message;

class MessageController extends Controller
{
/**
* Store a new message
*
* @param \Illuminate\Http\SendMessage $request
* @return \Illuminate\Http\Response
*/
public function store(SendMessage $request)
{
$data = $request->validated();

$threadId = $data['thread_id'];
$authUser = $request->user()->id;

// Check if a thread exist and if the user is related to it
$thread = MessageThread::where('id', $threadId)
->where(function($query) use ($authUser) {
$query->where('user_one', $authUser)
->orWhere('user_two', $authUser);
})
->firstOrFail();

// Create the message
$message = $thread->messages()->create([
'user_id' => $authUser,
'body' => $data['body'],
'messageable_type' => 'MessageThread',
'messageable_id' => $threadId
]);

return response()->json($message);
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$authUser = request()->user()->id;

$message = Message::where('user_id', $authUser)
->where('id', $id)
->with('messageable')
->firstOrFail();

$thread = $message->messageable;

if($thread->user_one == $authUser || $thread->user_two == $authUser) {
$message = $message->delete();

return response()->json($message);
}
}

/**
* Fetch messages from a message-thread
*
* @param $id - I.D of the message-thread
* @return array of paginated messages
*/
public function messages($id)
{
$authUser = request()->user()->id;

// Check if a thread exist and if the user is related to it
$thread = MessageThread::where('id', $id)
->where(function($query) use ($authUser) {
$query->where('user_one', $authUser)
->orWhere('user_two', $authUser);
})
->firstOrFail();

// Fetch all messages from this thread
$messages = $thread
->messages()
->select('id','user_id','body','created_at','updated_at')
->with('sender:id,name')
->paginate();

return response()->json($messages);
}
}
60 changes: 40 additions & 20 deletions app/Http/Controllers/Api/MessageThreadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\MessageThread;
use App\Message;

class MessageThreadsController extends Controller
{
Expand All @@ -20,41 +21,48 @@ public function index()

$data = MessageThread::where('user_one', $authenticatedUserId)
->orWhere('user_two', $authenticatedUserId)
->with('sender', 'receiver')
->with('sender:id,name', 'receiver:id,name')
->orderBy('last_activity', 'asc')
->paginate();

$data = json_encode($data);
$data = json_decode($data);

if(!empty($data->data))
{
return response()->json($data);
}

return response()->json($data, 204);
return response()->json($data);
}

/**
* Store a newly created resource in storage.
* Create a message thread
*
* @param \Illuminate\Http\Request $request
* @param $request - Data of the post request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$authUser = $request->user()->id;
$secondUser = $request->secondUser;

$data = MessageThread::create([
'user_one' => $authUser,
'user_two' => $secondUser,
'last_activity' => Carbon::now()
]);

return response()->json($data);
}

/**
* Display the specified resource.
* Select the specified message thread.
*
* @param int $id
* @return \Illuminate\Http\Response
* @param int $id - I.D of the message thread
* @return \Illuminate\Http\Response - return the paginated messages of this thread
*/
public function show($id)
{
//
$data = MessageThread::with('sender:id,name','receiver:id,name')->find($id);

// If thread does not exist, return 404
if(!$data)
return abort(404, 'Message Thread/Conversation not found');

return response()->json($data);
}

/**
Expand All @@ -70,13 +78,25 @@ public function update(Request $request, $id)
}

/**
* Remove the specified resource from storage.
* Delete a message thread if found
*
* @param int $id
* @param int $id - I.D of the message thread
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
$authUser = request()->user()->id;

$thread = MessageThread::where('id', $id)
->where(function($query) use ($authUser) {
$query->where('user_one', $authUser)
->orWhere('user_two', $authUser);
})
->delete();

if($thread)
return response()->json(['message'=>'Successfully deleted']);

return response()->json(['message'=>'Message thread not found'], 404);
}
}
26 changes: 13 additions & 13 deletions app/Http/Controllers/Api/PostController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\API;
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
Expand All @@ -11,19 +11,19 @@ class PostController extends Controller
{
/**
* Fetch all posts of the authenticated user
*
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$data = Post::where('user_id', $request->user()->id)
->with('user:id,name')
->orderBy('id', 'desc')
->get();
->get();

if( count($data) >= 1 )
return response()->json($data, 200);

return response()->json(['message' => 'No posts yet'], 204);
}

Expand All @@ -42,7 +42,7 @@ public function store(Posts $request)

// Retrieve the newly created post with user details
$newData = Post::where('id', $dataInsert->id)->with('user:id,name')->first();

if($newData)
return response()->json(['message' => 'Successfully Posted', 'data' => $newData], 201);
}
Expand All @@ -59,16 +59,16 @@ public function show($id)

if($data)
return response()->json($data, 200);

return response()->json(['message' => 'Post not found'], 404);

}

/**
* Update the specified resource in storage.
* Warning! when using this function
* Warning! when using this function
* please add (_method: PUT or PATCH) in data of http request
*
*
* @param App\Post $request
* @param int $id
* @return \Illuminate\Http\Response
Expand All @@ -80,9 +80,9 @@ public function update(Posts $request, $id)

// Checks if post exist
$presentData = Post::findOrFail($id);

//Check if user is the owner of the post
if($presentData->user_id != $request->user()->id)
if($presentData->user_id != $request->user()->id)
return response()->json(['message' => 'User unauthorized'], 401);

$presentData->update($dataUpdate);
Expand All @@ -102,12 +102,12 @@ public function update(Posts $request, $id)
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $id)
{
{
$user = $request->user();

// Check if post exists
$post = Post::findOrFail($id);

// Check if the user that requested to delete is the owner of the post
$authorize = $user->id == $post->user_id;

Expand Down
Loading

0 comments on commit 3b7ca2b

Please sign in to comment.