Skip to content

Commit

Permalink
Merge pull request #4 from bbzblit/yanni8/import
Browse files Browse the repository at this point in the history
add import function
  • Loading branch information
Yanni8 authored Mar 31, 2024
2 parents 9578dec + cfd407f commit 228fcb8
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 82 deletions.
44 changes: 44 additions & 0 deletions app/Http/Controllers/SetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\Set\Import;
use App\Models\Set;

class SetController extends Controller
{
public function import(Import $request)
{
$data = $request->string("export");

$data = trim($data);
$set = Set::create([
"title" => "Imported Set",
"user_id" => auth()->id(),
]);

$cards = [];
foreach (explode("\n", $data) as $line) {
$line = trim($line);
if (empty($line)) {
continue;
}
$line = explode("\t", $line);
if (count($line) < 2) {
continue;
}
$key = $line[0];
$value = $line[1];

$cards[] = [
"key" => $key,
"value" => $value,
"set_id" => $set->id,
];
}

$set->cards()->createMany($cards);

return $set;
}
}
29 changes: 29 additions & 0 deletions app/Http/Requests/Set/Import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests\Set;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class Import extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Auth::check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
"export" => ["required", "string"]
];
}
}
11 changes: 0 additions & 11 deletions app/Models/FalshCard.php

This file was deleted.

24 changes: 24 additions & 0 deletions app/Models/FlashCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class FlashCard extends Model
{
use HasFactory, HasUuids;

protected $table = 'flash_cards';

protected $fillable = [
"key",
"value",
'set_id',
];

public function post(){
return $this->belongsTo(Set::class);
}
}
18 changes: 17 additions & 1 deletion app/Models/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Set extends Model
{
use HasFactory;
use HasFactory, HasUuids;

protected $table = 'sets';

protected $fillable = [
"title",
"user_id"
];

public function post(){
return $this->belongsTo(User::class);
}

public function cards(){
return $this->hasMany(FlashCard::class);
}
}
10 changes: 5 additions & 5 deletions database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->uuid("id");
$table->string('first_name', [255]);
$table->string("last_name", [255]);
$table->string('email', [255])->unique();
$table->string('password', [255]);
$table->uuid("id")->primary();
$table->string('first_name');
$table->string("last_name");
$table->string('email')->unique();
$table->string('password');
$table->timestamp('last_login_at')->nullable();
$table->rememberToken();
$table->timestamps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public function up(): void
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->uuid("id");
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
Expand Down

This file was deleted.

5 changes: 4 additions & 1 deletion database/migrations/2024_03_05_094610_create_sets_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
public function up(): void
{
Schema::create('sets', function (Blueprint $table) {
$table->id();
$table->uuid("id")->primary();
$table->timestamps();
$table->string("title");
$table->uuid("user_id");
$table->foreign("user_id")->references("id")->on("users")->cascadeOnDelete();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
*/
public function up(): void
{
Schema::create('falsh_cards', function (Blueprint $table) {
$table->id();
Schema::create('flash_cards', function (Blueprint $table) {
$table->uuid("id")->primary();
$table->timestamps();
$table->string('key');
$table->string('value');
$table->uuid('set_id');
$table->foreign('set_id')->references('id')->on('sets')->cascadeOnDelete();
});
}

Expand All @@ -22,6 +26,6 @@ public function up(): void
*/
public function down(): void
{
Schema::dropIfExists('falsh_cards');
Schema::dropIfExists('flash_cards');
}
};
66 changes: 42 additions & 24 deletions resources/js/Components/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,51 @@
<div class="bg-transparent border-none relative flex flex-col w-80">
<label class="pb-2">{{ label }}</label>
<div class="relative">
<input v-bind="$attrs" :value="modelValue"
@input="modelValue = $event.target.value; $emit('update:modelValue', $event.target.value)" :maxlength="max"
:minlength="min" :required="required" :type="state_type" :placeholder="placeholder"
class="bg-[#333333] px-4 py-2 rounded-md w-full" @invalid="invalid">
<i v-if="isPassword" class="fa-solid fa-eye absolute right-2 bottom-[13px] cursor-pointer"
@click="togglePassword"></i>
<input
v-bind="$attrs"
:value="modelValue"
@input="onInput($event.target.value)"
:maxlength="max"
:minlength="min"
:required="required"
:type="state_type"
:placeholder="placeholder"
class="bg-[#333333] px-4 py-2 rounded-md w-full"
@invalid="invalid"
/>
<i
v-if="isPassword"
class="fa-solid fa-eye absolute right-2 bottom-[13px] cursor-pointer"
@click="togglePassword"
></i>
</div>
<div class="min-h-6">
<label class="text-red-600 font-bold text-sm mt-2">{{ error ? error : customError }}</label>
<label class="text-red-600 font-bold text-sm mt-2">{{
error ? error : customError
}}</label>
</div>
</div>
</template>

<script>
export default {
name: 'FormInput',
emits: ['update:modelValue'],
name: "FormInput",
emits: ["update:modelValue"],
props: {
label: {
type: String,
},
type: {
type: String,
default: 'text'
default: "text",
},
placeholder: {
type: String,
default: ''
default: "",
},
required: {
type: Boolean,
default: false
default: false,
},
max: {
type: Number,
Expand All @@ -42,21 +55,20 @@ export default {
type: Number,
},
modelValue: {
default: ''
default: "",
},
customError: {
type: String,
default: ''
}
default: "",
},
},
data() {
return {
error: "",
isPassword: this.type === 'password',
state_type: this.type
}
isPassword: this.type === "password",
state_type: this.type,
};
},
methods: {
Expand All @@ -77,9 +89,15 @@ export default {
}
},
togglePassword() {
this.state_type = this.state_type === 'password' ? 'text' : 'password';
}
}
}
this.state_type =
this.state_type === "password" ? "text" : "password";
},
onInput(value) {
this.error = "";
this.$emit("update:modelValue", value);
},
},
};
</script>
<style></style>
<style></style>
9 changes: 7 additions & 2 deletions resources/js/Pages/Auth/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<form-group :disabled="!isValid" title="Sign Up" label="Name" for="name">
<form-input v-model="values.first_name" name="first_name" type="text" label="First Name" placeholder="Your Name" required />
<form-input v-model="values.last_name" name="last_name" type="text" label="Last Name" placeholder="Your Name" required />
<form-input v-model="values.email" name="email" type="email" label="Email" placeholder="Your Email" required @update:modelValue="resetErrors" />
<form-input v-model="values.email" name="email" :customError="emailNotMachError" type="email" label="Email" placeholder="Your Email" required @update:modelValue="resetErrors" />
<form-input v-model="values.password" name="password" type="password" label="Password" placeholder="Your Password" required />
<form-input v-model="values.password_confirmation" :customError="customError" name="password_confirmation" type="password" label="Confirm Password" placeholder="Confirm Password" required />
<a href="login" class="text-gray-300 hover:text-gray-400 font-bold pb-2">Already have an account?</a>
Expand Down Expand Up @@ -49,7 +49,12 @@ export default {
if (!this.isValid){
return 'Passwords do not match';
}
if (this.errors.length > 0){
if (this.errors.length > 0 && !this.errors[0].includes('email')){
return this.errors[0];
}
},
emailNotMachError() {
if (this.errors.length > 0 && this.errors[0].includes('email')){
return this.errors[0];
}
}
Expand Down
Loading

0 comments on commit 228fcb8

Please sign in to comment.