Commit a8cba220 authored by Tomáš Biloš's avatar Tomáš Biloš
Browse files

Merge branch 'xbilos/fixes' into 'main'

fix: password, skill creation

See merge request !84
parents a8639a58 8e6c0fbf
......@@ -4,8 +4,11 @@ import org.springframework.data.repository.CrudRepository;
import cz.fi.muni.pa165.seminar4.group7.entity.Skill;
import java.util.Optional;
/**
* @author Tomáš Biloš
*/
public interface SkillDao extends CrudRepository<Skill, String> {
Optional<Skill> findByName(String name);
}
......@@ -100,7 +100,12 @@ public class AgentFacadeImpl implements AgentFacade {
private Set<Skill> createSkills(Set<Skill> skills) {
for (var skill : skills) {
if (skill.getId() == null) {
skill.setId(UUID.randomUUID().toString());
var optSkill = skillService.findByName(skill.getName());
if (optSkill.isEmpty()) {
skill.setId(UUID.randomUUID().toString());
} else {
skill.setId(optSkill.get().getId());
}
}
}
return skills;
......
......@@ -27,6 +27,16 @@ public interface SkillService {
*/
Optional<Skill> findById(String id);
/**
* Look for skill with specific name.
*
* @param name
* name to look for.
*
* @return Optional of skill if skill with name was found, optional of empty otherwise.
*/
Optional<Skill> findByName(String name);
/**
* Creates skill.
*
......
......@@ -31,6 +31,11 @@ public class SkillServiceImpl implements SkillService {
return skillDao.findById(id);
}
@Override
public Optional<Skill> findByName(String name) {
return skillDao.findByName(name);
}
@Override
public void create(Skill skill) {
if (skillDao.findById(skill.getId()).isPresent()) {
......@@ -41,7 +46,7 @@ public class SkillServiceImpl implements SkillService {
@Override
public void deleteById(String id) {
if (!skillDao.findById(id).isPresent()) {
if (skillDao.findById(id).isEmpty()) {
throw new IllegalArgumentException("Record for given skill doesn't exist.");
}
skillDao.deleteById(id);
......@@ -49,7 +54,7 @@ public class SkillServiceImpl implements SkillService {
@Override
public void update(Skill skill) {
if (!skillDao.findById(skill.getId()).isPresent()) {
if (skillDao.findById(skill.getId()).isEmpty()) {
throw new IllegalArgumentException("Record for given skill doesn't exist.");
}
skillDao.save(skill);
......
......@@ -16,6 +16,8 @@
</mat-toolbar>
<div *ngIf="agent" class="main-container">
<div *ngIf="errorMessage" class="text-red">{{ errorMessage }}</div>
<form [formGroup]="edit" class="flex flex-col px-4">
<mat-form-field>
<mat-label>Agent name</mat-label>
......@@ -32,7 +34,7 @@
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-form-field *ngIf="creatingNewAgent">
<mat-label>Password</mat-label>
<input matInput formControlName="password" type="password" />
<mat-error>Must be at least 6 characters long</mat-error>
......@@ -50,9 +52,10 @@
<div mat-subheader>Codenames</div>
<mat-list-item *ngFor="let codeName of agent.codeNames">
<mat-icon mat-list-icon>badge</mat-icon>
<h2 mat-line> {{codeName.codeName}} </h2>
<h2 mat-line>{{ codeName.codeName }}</h2>
</mat-list-item>
</mat-list>
<mat-list>
<mat-list-item>
<mat-icon mat-list-icon></mat-icon>
<mat-form-field mat-line>
......@@ -69,7 +72,7 @@
<div mat-subheader>Skills</div>
<mat-list-item *ngFor="let skill of agent.skills">
<mat-icon mat-list-icon>plumbing</mat-icon>
<h2 mat-line> {{skill.name}} </h2>
<h2 mat-line>{{ skill.name }}</h2>
</mat-list-item>
<mat-list-item>
......@@ -84,12 +87,16 @@
</mat-list-item>
<mat-divider></mat-divider>
</mat-list>
<button color="accent" mat-fab class="!fixed right-8 bottom-20" aria-label="Save agent details"
disabled="{{ !edit.valid }}" (click)="saveAgent()">
<button
color="accent"
mat-fab
class="!fixed right-8 bottom-20"
aria-label="Save agent details"
disabled="{{ !edit.valid }}"
(click)="saveAgent()"
>
<mat-icon>done</mat-icon>
</button>
</div>
......@@ -23,18 +23,16 @@ export class EditAgentComponent implements OnInit {
roles = ['REGULAR', 'ADMIN', 'SUPERVISOR'];
selectedRole = 'REGULAR';
state: RequestState = RequestState.SUCCESS;
errorMessage: String = '';
errorMessage: String | undefined;
edit = new FormGroup({
name: new FormControl('', [
Validators.pattern(/[a-zA-Z]{3,}/),
Validators.required,
]),
training: new FormControl(''),
password: new FormControl('', [
Validators.required,
Validators.pattern(/.{6,}/),
]),
password: new FormControl(''),
});
creatingNewAgent = false;
skillColumns = ['skill'];
......@@ -100,24 +98,47 @@ export class EditAgentComponent implements OnInit {
input.value = ""
}
isPasswordValid() {
return this.edit.get('password')?.value?.length >= 6;
}
async saveAgent() {
if (this.creatingNewAgent && !this.isPasswordValid()) {
console.log(this.edit.get('password')?.value);
this.errorMessage = 'Invalid password';
return;
}
this.agent.name = this.edit.get('name')?.value;
this.agent.training = this.edit.get('training')?.value;
this.agent.agentRole = this.selectedRole;
this.agent.password = this.edit.get('password')?.value;
if (this.creatingNewAgent) {
this.agent = await firstValueFrom(
this.httpClient.post<Agent>(`${environment.apiUrl}/agents`, this.agent)
);
this.router.navigate(['auth', 'agents']);
try {
this.agent = await firstValueFrom(
this.httpClient.post<Agent>(
`${environment.apiUrl}/agents`,
this.agent
)
);
this.router.navigate(['auth', 'agents']);
} catch (error) {
this.errorMessage = 'Failed to create agent';
this.state = RequestState.ERROR;
}
} else {
this.agent = await firstValueFrom(
this.httpClient.put<Agent>(
`${environment.apiUrl}/agents/${this.agent.id}`,
this.agent
)
);
this.router.navigate(['auth', 'agent', this.agent.id]);
try {
this.agent = await firstValueFrom(
this.httpClient.put<Agent>(
`${environment.apiUrl}/agents/${this.agent.id}`,
this.agent
)
);
this.router.navigate(['auth', 'agent', this.agent.id]);
} catch (error) {
this.errorMessage = 'Failed to update agent';
this.state = RequestState.ERROR;
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment