Skip to content

Commit

Permalink
Fixed issue #173 - lots of MP bugs squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph-Meyer committed Feb 8, 2019
1 parent e241059 commit 457d4f7
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface LevelRepository extends JpaRepository<Level, Long> {
Level findById(Long id);

Level findFirstByMinXpIsLessThanEqualOrderByLevelDesc(Long xp);

Level findFirstByLevelOrderByLevelDesc(int level);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -13,6 +17,8 @@
@Service
public class ArtefactService {

private static final Logger LOGGER = LoggerFactory.getLogger(ArtefactService.class);

@Autowired
private ArtefactRepository artefactRepository;

Expand All @@ -34,24 +40,40 @@ public Artefact getArtefact(final long id) {
return artefactRepository.findOne(id);
}

@Transactional
public Artefact createArtefact(final Artefact artefact) {
LOGGER.info("Creating new artefact " + artefact.getName());
Level minLevel = artefact.getMinLevel();
Level existingLevel = levelService.findByLevel(minLevel.getLevel());
if (existingLevel != null) {
artefact.setMinLevel(existingLevel);
} else {
LOGGER.info("Artefact Level " + minLevel.getLevel() + " does not exist yet - creating it...");
levelService.createLevel(minLevel);
artefact.setMinLevel(minLevel);
}
return artefactRepository.save(artefact);
}

@Transactional
public Artefact updateArtefact(final Long id, final Artefact artefactDto) {
LOGGER.info("Updating artefact " + id);
final Artefact artefact = artefactRepository.findOne(id);
artefact.setName(artefactDto.getName());
artefact.setIcon(artefactDto.getIcon());
artefact.setPrice(artefactDto.getPrice());
artefact.setDescription(artefactDto.getDescription());
artefact.setQuantity(artefactDto.getQuantity());
artefact.setMinLevel(levelService.findById(artefactDto.getMinLevel().getId()));
artefact.setSkills(artefactDto.getSkills());
int minLevel = artefactDto.getMinLevel().getLevel();
Level newLevel = levelService.findByLevel(minLevel);
artefact.setMinLevel(newLevel);
return artefactRepository.save(artefact);
}

public Artefact buyArtefact(Artefact artefact, final User user) {

@Transactional
public synchronized Artefact buyArtefact(Artefact artefact, final User user) {
LOGGER.info("User " + user.getId() + " tries to buy artefact " + artefact.getId());
// If developer has TOO LITTLE GOLD, Then the purchase is canceled
final long gold = user.getGold() - artefact.getPrice();
if (gold < 0) {
Expand Down Expand Up @@ -85,6 +107,7 @@ public Artefact buyArtefact(Artefact artefact, final User user) {

artefact.setQuantity(artefact.getQuantity() - 1);
artefact = artefactRepository.save(artefact);
LOGGER.info("User " + user.getId() + " successfully bought artefact " + artefact.getId());
return artefact;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ public Level getLevelByUserXp(final Long xp) {
public Level findById(final Long id) {
return levelRepository.findById(id);
}

public void createLevel(Level newLevel) {
levelRepository.save(newLevel);
}

public Level findByLevel(int level) {
return levelRepository.findFirstByLevelOrderByLevelDesc(level);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -19,6 +21,7 @@ public class SkillService {
@Autowired
private ArtefactRepository artefactRepository;

@Transactional
public Skill createSkill(final Skill skillDto) {
final Skill skill = new Skill();
skill.setName(skillDto.getName());
Expand All @@ -31,6 +34,7 @@ public List<Skill> getSkillsForArtefact(final Artefact a) {
return artefactRepository.findOne(a.getId()).getSkills();
}

@Transactional
public void deleteSkill(final Skill skill) {
if (skill != null) {
skillRepository.delete(skill);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class GamemasterArtefactCreateComponent implements OnInit {
this.quantity = 1;
}

if (this.name && this.min && this.price && this.quantity && this.skills.length > 0) {
if (this.name && this.min && this.price && this.quantity) {
const artefact = {
name: this.name,
price: this.price,
Expand All @@ -105,7 +105,7 @@ export class GamemasterArtefactCreateComponent implements OnInit {
skills: this.skills,
icon: this.icon,
minLevel: {
min: this.min
level: this.min
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class GamemasterArtefactEditComponent implements OnInit {
this.filter();
});
this.name = this.artefact.name;
this.min = this.artefact.minLevel.minXp;
this.min = this.artefact.minLevel.level;
this.price = this.artefact.price;
this.description = this.artefact.description;
this.quantity = this.artefact.quantity;
Expand All @@ -85,18 +85,21 @@ export class GamemasterArtefactEditComponent implements OnInit {
}

updateArtefact() {
console.log('Checking artefact update...');
if (this.name && this.min && this.price) {
this.artefact.name = this.name;
this.artefact.price = this.price;
this.artefact.quantity = this.quantity;
this.artefact.description = this.description;
this.artefact.icon = this.icon;
this.artefactService.setMinLevel(this.artefact, this.min);
this.artefact.minLevel.level = this.min;

this.artefactService.updateArtefact(this.artefact).then(() => {
this.artefactService.getData();
this.dialogRef.close();
});
} else {
console.error('Error updating artefact: ' + this.name);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
<ng-template tdDataTableTemplate="edit" let-value="value" let-row="row" let-column="column">
<div layout="row">
<button mat-icon-button (click)="editArtefact(row)" [matTooltip]="'GLOBAL.EDIT' | translate"
[matTooltipClass]="'tooltipMultiline'" matTooltipPosition="below">
[matTooltipClass]="'tooltipMultiline'" matTooltipPosition="below">
<i class="ra ra-quill-ink ra-2x"></i>
</button>
<button mat-icon-button (click)="deleteArtefact(row)" [matTooltip]="'GLOBAL.DELETE' | translate"
[matTooltipClass]="'tooltipMultiline'" matTooltipPosition="below">
<i class="ra ra-broken-skull ra-2x"></i>
</button>
</div>
</ng-template>
</td-data-table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {ArtefactService} from './../../../../services/artefact.service';
import {Component, OnInit} from '@angular/core';
import {Artefact} from '../../../../Interfaces/Artefact';
import { TranslateService } from '@ngx-translate/core';
import { updateLocale } from 'moment';

@Component({
selector: 'app-gamemaster-marketplace',
Expand All @@ -28,7 +29,7 @@ export class GamemasterMarketplaceComponent implements OnInit {
{name: 'name', label: 'name'},
{name: 'price', label: 'Price (in Gold)'},
{name: 'quantity', label: 'Quantity'},
{name: 'minLevel.min', label: 'min. Level'},
{name: 'minLevel.level', label: 'min. Level'},
{name: 'skills', label: 'Skills'},
{name: 'edit', label: ''}
];
Expand Down Expand Up @@ -58,15 +59,11 @@ export class GamemasterMarketplaceComponent implements OnInit {
{name: 'name', label: col_names.NAME},
{name: 'price', label: col_names.PRICE},
{name: 'quantity', label: col_names.QUANTITY},
{name: 'minLevel.min', label: col_names.MIN_LEVEL},
{name: 'minLevel.level', label: col_names.MIN_LEVEL},
{name: 'skills', label: col_names.SKILLS},
{name: 'buy', label: ''}]
{name: 'edit', label: ''}]
});
this.artefactService.artefacts$.subscribe(artefacts => {
this.artefacts = artefacts;
this.filter();
});
this.artefactService.getData();
this.update();
}

newArtefact() {
Expand All @@ -84,6 +81,24 @@ export class GamemasterMarketplaceComponent implements OnInit {
});
}

deleteArtefact(artefact: Artefact) {
var msg = "";
this.translateService.get('GLOBAL.CONFIRMATION_MESSAGE').subscribe(translateMsg => msg = translateMsg);
if(confirm(msg)) {
this.artefactService.deleteArtefact(artefact).then(() => {
this.update();
});
}
}

update() {
this.artefactService.artefacts$.subscribe(artefacts => {
this.artefacts = artefacts;
this.filter();
});
this.artefactService.getData();
}

sort(sortEvent: ITdDataTableSortChangeEvent): void {
this.sortBy = sortEvent.name;
this.sortOrder = sortEvent.order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class MarketplacePageComponent implements OnInit {
{name: 'name', label: 'Name'},
{name: 'price', label: 'Price(Gold)'},
{name: 'quantity', label: 'Quantity'},
{name: 'minLevel.min', label: 'min. Level'},
{name: 'minLevel.level', label: 'min. Level'},
{name: 'buy', label: ''}
];

Expand Down Expand Up @@ -61,7 +61,7 @@ export class MarketplacePageComponent implements OnInit {
{name: 'name', label: col_names.NAME},
{name: 'price', label: col_names.PRICE},
{name: 'quantity', label: col_names.QUANTITY},
{name: 'minLevel.min', label: col_names.MIN_LEVEL},
{name: 'minLevel.level', label: col_names.MIN_LEVEL},
{name: 'buy', label: ''}]
});
this.artefactService.artefactsforMarkteplace$.subscribe(artefacts => {
Expand Down
10 changes: 6 additions & 4 deletions sonarQuest-frontend/src/app/services/artefact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class ArtefactService {
}

updateArtefact(artefact: any): Promise<Artefact> {
console.log('Updating artefact ' + artefact.id);
return this.http.put<Artefact>(`${environment.endpoint}/artefact/${artefact.id}`, artefact)
.toPromise()
.catch(this.handleError);
Expand All @@ -60,6 +61,11 @@ export class ArtefactService {
.catch(this.handleError);
}

deleteArtefact(artefact: Artefact): Promise<any> {
return this.http.delete(`${environment.endpoint}/artefact/${artefact.id}`)
.toPromise()
}

private handleError(error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
Expand All @@ -72,8 +78,4 @@ export class ArtefactService {
console.error(errMsg);
return Promise.reject(errMsg);
}

setMinLevel(artefact: Artefact, min: number) {
artefact.minLevel.minXp = min
}
}

0 comments on commit 457d4f7

Please sign in to comment.