I created the following tables below to represent the hierarchy of a specific job. Then I created some CRUD functions in java to "createJob", "getJob", "updateJob" and "deleteJob" in these tables.
Now I would like to implement the data I get back from these operations on a user interface. I'm looking for the simplest UI where I could probably have the job listed in a dropdown list and then based on my selection I can choose the job type then based on that selection choose the job specification. These CRUD functions can be performed on each dropdown. To add a new Job, rename it, etc.
What's the simplest way to do this? I've been looking around for some APIs because I believe this would be something that's common. There is probably a more descriptive term for these types of drop downs which would help my search.
EDIT After further research, what I am looking for is an "Editable Cascading drop down list".
EDIT 2 I was able to see some good examples using Angular to create the cascading drop down list. I was able to create the app.component.ts and app.component.html below. I can get the dropdowns to display but they are not cascading based on the Job selected. What am I doing wrong in my code? Also, soon as this is working, how can I prepare the code to make use of the java functions I created earlier rather than the hard coded values I have?
+---------+---------+
| Id | Job |
+---------+---------+
| 1 |Developer |
|
| 2 |QA Tester |
+---------+---------+---------+-----+
| Id | Job_Type | Job_Id
+---------+---------+---------+-----+
| 1 |Java Developer | 1 |
| 2 |JS Developer | 1 |
| 3 |FrontEnd Tester | 2 |
| 4 |Backend Tester | 2 |
| 5 |Php Developer | 1 |
+---------+---------+---------+----+-----------+
| Id | Job_Specification | Job_Type_Id
+---------+---------+---------+----+-----------+
| 1 |Spring Developer | 1 |
| 2 |Angular Developer | 2 |
| 3 |UI Tester | 3 |
| 4 |Mobile Tester | 3 |
| 5 |Hibernate Developer | 1 |
APP.COMPONENT.TS
import { Component } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Roles!';
selectedRole= 0;
selectedProviderType = 0;
providerType = [];
classification = [];
onSelectRole(role_id: number) {
this.selectedRole = role_id;
this.selectedProviderType = 0;
this.classification = [];
this.providerType = this.getProviderType().filter((item) => {
return item.role_id === Number(role_id)
});
}
onSelectProviderType(providerType_id: number) {
this.selectedProviderType = providerType_id;
this.classification = this.getClassification().filter((item) => {
return item.providerType_id === Number(providerType_id)
});
}
getRoles() {
return [
{ id: 1, name: 'Developer' },
{ id: 2, name: 'QA Tester' },
];
}
getProviderType() {
return [
{ id: 1, role_id: 1, name: 'Java Developer' },
{ id: 2, role_id: 1, name: 'JS Developer' },
{ id: 3, role_id: 2, name: 'FrontEnd Tester' },
{ id: 4, role_id: 2, name: 'Backend Tester' },
{ id: 5, role_id: 3, name: 'Php Developer' },
]
}
getClassification() {
return [
{ id: 1, providerType_id: 1, name: 'Spring Developer' },
{ id: 2, providerType_id: 1, name: 'Angular Developer ' },
{ id: 3, providerType_id: 1, name: 'UI Tester' },
{ id: 4, providerType_id: 1, name: 'Mobile Tester' },
{ id: 5, providerType_id: 2, name: 'Hibernate Developer' },
]
}
}
APP.COMPONENT.HTML
</div>
<div class="form-group">
<label class="control-label" for="Role">Role:</label>
<select *ngIf="getRoles()" [(ngModel)]="selectedRole" (change)="onSelectRole($event.target.value)" class="form-control input-lg" id="role">
<option value="0">Select Role</option>
<option *ngFor="let role of getRoles()" value= {{role.id}}>{{role.name}}</option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="ProviderType">Provider Type:</label>
<select *ngIf="getProviderType()" [(ngModel)]="selectedProviderType" (change)="onSelectProviderType($event.target.value)" class="form-control input-lg" id="providerType">
<option value="0">Select Provider Type</option>
<option *ngFor="let providerType of providerTypes" value= {{providerType.id}}>{{providerType.name}}</option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="Classification">Classification:</label>
<select class="form-control input-lg" id="classification">
<option *ngIf="!selectedProviderType" value="0">Select Classification</option>
<option *ngFor="let classification of classifications" value= {{classification.id}}>{{classification.name}}</option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="Specification">Specification:</label>
<select class="form-control input-lg" id="city">
<option *ngIf="!selectedClassification" value="0">Select Specification</option>
<option *ngFor="let specification of specifications" value= {{specification.id}}>{{specification.name}}</option>
</select>
</div>
Related
I want to find all records in a MySql table which was created within a certain range of date.
So I wrote:
import { Sequelize, Model, DataTypes, Op } from 'sequelize';
const sequelize = new Sequelize({
// some db connection config
dialect: 'mysql'
})
class Patient extends Model {
public guid!: number;
public name!: string;
public recordState: number = 0;
public createdAt?: Date;
public updatedAt?: Date
}
Patient.init({
guid: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false
},
name: { type: DataTypes.STRING, allowNull: false },
recordState: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE
}, {
sequelize,
modelName: 'Patient',
timestamps: false
})
Patient.findAll({
where: {
createdAt: {
[Op.between]: [new Date('2020-02-02'), new Date()]
}
}
})
But, when I try to compile it with tsc, it reports error like:
sequelize.ts:50:5 - error TS2322: Type '{ [between]: Date[]; }' is not assignable to type 'string | number | boolean | WhereAttributeHash | AndOperator | OrOperator | Literal | Where | Fn | Col | WhereOperators | Buffer | WhereGeometryOptions | (string | ... 2 more ... | Buffer)[]'.
Types of property '[Op.between]' are incompatible.
Type 'Date[]' is not assignable to type 'string | number | boolean | [number, number] | WhereAttributeHash | AndOperator | OrOperator | Literal | Where | ... 5 more ... | (string | ... 2 more ... | Buffer)[]'.
Type 'Date[]' is not assignable to type '(string | number | WhereAttributeHash | Buffer)[]'.
Type 'Date' is not assignable to type 'string | number | WhereAttributeHash | Buffer'.
Type 'Date' is not assignable to type 'WhereAttributeHash'.
Index signature is missing in type 'Date'.
50 createdAt: {
~~~~~~~~~
Found 1 error.
It seems I cannot use Op.between with a date range? But it's ok when I wrote similar code in JS.
So I wonder if there is really something wrong in my TS code or just a missing in the type definition, or maybe using Op.between with dates is not recommended?
You're passing a date object instead of a string. Do this:
Patient.findAll({
where: {
createdAt: {
[Op.between]: [new Date('2020-02-02').toISOString(), new Date().toISOString()]
}
}
})
in my case i had:
createdAt: {
[Op.between]: [
new Date(startDate).toISOString(),
new Date(`${endDate} 23:59:59`).toISOString()
]
}
and worked changing to:
createdAt: {
[Op.and]: [
{
[Op.lt]: new Date(`${endDate} 23:59:59`),
[Op.gt]: new Date(startDate)
}
]
}
I am stuck on SQL Relationships. I have been search for this solution for 3-4 hours with little progress. I have 2 tables in which I want to create a one to many relationships and i want to make a user hobbies API.
tb_user
+----------+--------------------------+
| id_user | name |
+----------+--------------------------+
| 1 | Irsyad Abdul |
| 2 | Abdul Hamid |
| 3 | Darussalam |
+----------+--------------------------+
tb_skillForeign Key at id _user
+----------+--------------+---------------+--------------------+
| id_skill | id_user(FK) | skill | level |
+----------+--------------+---------------+--------------------+
| 1 | 1 | Drawing | Intermediate |
| 2 | 1 | Coding | Beginner |
| 3 | 1 | Photoshop | Intermediate |
| 4 | 2 | Basketball | Amateur |
+----------+--------------+---------------+--------------------+
I want to create a user API that return their name and their skills in JSON. here is my code
app.get('/user',(req,res)=>{
let sql1 = "SELECT * FROM tb_user";
conn.query(sql1,(err,result1)=>{
if (err) throw err;
else{
for(var i = 0;i<result1.length;i++){
let sql2 = "SELECT skill,level FROM tb_skill WHERE tb_skill.id_user='"+result1[i].id_user+"'";
conn.query(sql2,(err,result2)=>{
if(err) throw err
else{
result1[i].skills=result2;
}
});
}
var data = {
'status' : 200,
'data' : result1
};
res.json(data);
res.end;
}
});
});
i want the JSON return like this on Postman
{
"status": 200,
"data": [
{
"id_user": 1,
"name": "Irsyad Abdul",
"skills": [
{
"skill": "Drawing",
"level": "Intermediate"
},
{
"skill": "Coding",
"level": "Beginner"
},
{
"skill": "Photoshop",
"level": "Intermediate"
}
]
},
{
"id_user": 2,
"name": "Abdul Hamid",
"skills": [
{
"skill": "Basketball",
"level": "Amateur"
}
]
},
{
"id_user": 3,
"name": "Darussalam",
"skills": []
}
]
}
You are calling a async callback function inside a for-loop, that's why you don't get result. There are many ways to do this, but I'm just trying help you to fix your code. You can try following code:
app.get('/user',(req,res)=>{
sql1 = "SELECT * FROM tb_user";
conn.query(sql1,(err,result1)=>{
if (err) throw err;
else{
for(var i = 0;i<result1.length;i++){
( (pos ) => {
let sql2 = "SELECT skill,level FROM tb_skill WHERE tb_skill.id_user='"+result1[pos].id_user+"'";
conn.query(sql2,(err,result2)=>{
if(err) throw err
else{
result1[pos].skills=result2;
}
});
} ) (i );
}
var data = {
'status' : 200,
'data' : result1
};
res.json(data);
}
}
})
NB: Use Sequelize, It will give you much easy and accurate result
I have a json field in my database name is permissions {"read": true, "create": true}. Now, I want to show this in my blade view. I had try this way but it's show an error.
#foreach($users as $user)
<tr>
<td>{{$user->roles->permissions}}</td>
</tr>
#endforeach
show this Error Message.
Property [permissions] does not exist on this collection instance.
User Model
public function roles()
{
return $this->belongsToMany(Role::class,'user_roles');
}
I tested this scenario and was OK . i hope usefull for you :
my route :
Route::get('/test', function (Request $request) {
$users=\App\User::with('roles')->get();
return view('welcome',compact('users'));
});
define roles method inside user model :
public function roles()
{
return $this->belongsToMany(Role::class,'user_roles');;
}
and get permissions in view :
#foreach($users as $user)
#foreach($user->roles as $role)
<tr>
<td>{{$role}}</td>
</tr>
#endforea
#endforeach
I test with these tables
user_roles
user_id | role_id
-----------------
1 | 2
1 | 2
roles:
id | permissions
-----------------
1 | {"read": true, "create": true}
2 | {"read": true, "create": false}
users:
id | name | email | password | remember_token | created_at|updated_at
----------------------------------------------------------------------
1 | alihossein|ali#gmail.com|XXXXX|UIWKK67ei5CCuiv1OXilKY2aRkTfSqGLpqJch0F9YmenGSorsQGHVvWiX6kP| 2018-05-28 22:25:14 | 2018-05-28 22:25:14
I have a couple of tables I'm trying to associate in Sequelize -- a jobaids_sections_messages table containing messages a user enters, and a jobaids_sections_messages_levels table which is a static reference used by the messages table. They're set up like so
this.jobaidMessage = sequelize.define('jobaids_sections_messages', {
message: Sequelize.STRING,
attuid: Sequelize.STRING,
level: Sequelize.INTEGER
}, {
paranoid: true
});
this.jobaidMessageLevel = sequelize.define('jobaids_sections_messages_levels', {
name: Sequelize.STRING
}, {
timestamps: false
});
The jobaids_sections_messages_levels table is set up like so:
| id | name |
| --- | -------- |
| 1 | Critical |
| 2 | Major |
| 3 | Warning |
| 4 | Info |
I want to make is so that when I create a new message, I can pass the level in as a key to the jobaids_sections_messages_levels table, and upon retrieving a message, I get the level back as
{
...
level: {
id: 2,
name: 'Major'
}
}
How should I set my associations up here? So far, I have
this.jobaidMessageLevel.belongsTo(this.jobaidMessage, {
foreignKey: 'level'
});
Though I'm not sure about the reversal of this association. Would it be a "many-to-one" relationship of some sorts?
Thank you!
Your message has a single level and technically your levels can have many messages. So simply stating that your message hasOne level will do the association needed. Then when you pull down a message and include the level, it will come back.
this.jobaidMessage.hasOne(this.jobaidMessageLevel, {
foreignKey: 'levelId'
});
These are the two tables I want to end up with:
tableA (I already have data in this table)
id | initials | name
1 | ER | Eric Robinsn
2 | DD | David Dobson
tableB (nothing in here yet)
id | tableA_id | nickname
1 | 1 | Rick
2 | 1 | Ricky
3 | 1 | Mr. Bossman
4 | 2 | Dave
5 | 2 | Davey
This is the JSON I have:
[
{
name: "Eric Robinson",
initials: "ER",
nicknames: ["Rick", "Ricky", "Mr. Bossman"]
},
{
name: "David Dobson",
initials: "DD",
nicknames: ["Dave", "Davey"]
}
]
Inserting into tableA is very easy, you can do it like this with node-mysql:
vary connection = require("mysql");
var json = JSON.parse(require("./data.json"));
var sql = "INSERT INTO tableA(initials, name) VALUES ?";
connection.query(sql, json, callback);
But as a complete SQL noob how would I map the data into tableB? After some researching I'm not sure if I can do this with something like the following:
INSERT INTO tableB (tableA_id, nickname)
SELECT id
FROM tableA
Or maybe I need to include a left join? The part that confuses me the most is how to include the tableA_id part of the query into the statement. I've tried
INSERT INTO tableB (tableA_id, nickname)
SELECT id
FROM tableA
WHERE tableB.tableA_id = tableA.id //this is the part I don't get
This is just an abstracted example. Also, I'm using node-mysql so when I'm inserting into tableB the re-mapped JSON looks looks like this:
[
{
initials: "ER", nickname: "Rick"
},
{
initials: "ER", nickname: "Ricky"
},
{
initials: "ER", nickname: "Mr. Bossman"
},
{
initials: "DD", nickname: "Dave"
},
{
initials: "DD", nickname: "Davey"
}
]