Permalink
Browse files

add developer crud operations (front-end and back-end)

  • Loading branch information...
fsonmezay committed Oct 22, 2016
1 parent 5b23553 commit 6fdb1224f1b02b72a7748f90f2b0084dd05d2935
@@ -0,0 +1,39 @@
package com.ferdisonmezay.issuetracker.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ferdisonmezay.issuetracker.entity.DeveloperEntity;
import com.ferdisonmezay.issuetracker.service.DeveloperService;
@RestController
@RequestMapping("/developer")
public class DeveloperController {
@Autowired
private DeveloperService developerService;
@RequestMapping(value="list", method = RequestMethod.GET)
public List<DeveloperEntity> getDeveloperList() {
return developerService.getDevelopers();
}
@RequestMapping(value = "create", method = RequestMethod.POST)
public DeveloperEntity create(@RequestBody String name) {
return developerService.add(name);
}
@RequestMapping(value = "update/{id}", method = RequestMethod.PUT)
public DeveloperEntity update(@RequestBody DeveloperEntity developer) {
return developerService.update(developer);
}
@RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable(value = "id") Integer developerId) {
developerService.delete(developerId);
}
}
@@ -0,0 +1,10 @@
package com.ferdisonmezay.issuetracker.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.ferdisonmezay.issuetracker.entity.DeveloperEntity;
@Repository
public interface DeveloperDao extends JpaRepository<DeveloperEntity, Integer> {
}
@@ -0,0 +1,19 @@
package com.ferdisonmezay.issuetracker.entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID")
private Integer id;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
}
@@ -0,0 +1,18 @@
package com.ferdisonmezay.issuetracker.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="DEVELOPERS")
public class DeveloperEntity extends BaseEntity {
@Column(name="name")
private String name;
public DeveloperEntity() {}
public DeveloperEntity(String name) { this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
@@ -0,0 +1,25 @@
package com.ferdisonmezay.issuetracker.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ferdisonmezay.issuetracker.dao.DeveloperDao;
import com.ferdisonmezay.issuetracker.entity.DeveloperEntity;
@Service
public class DeveloperService{
@Autowired
private DeveloperDao developerDao;
public DeveloperEntity add(String name) {
DeveloperEntity developer = new DeveloperEntity(name);
return developerDao.save(developer);
}
public List<DeveloperEntity> getDevelopers(){ return developerDao.findAll(); }
public DeveloperEntity getById(Integer id) { return developerDao.findOne(id); }
public DeveloperEntity update(DeveloperEntity developer) { return developerDao.save(developer); }
public void delete(Integer developerId) { developerDao.delete(developerId); }
public Long getDeveloperCount() { return developerDao.count(); }
}
@@ -9,6 +9,10 @@
templateUrl: 'partials/main.html',
controller: 'MainController'
})
.when('/developer', {
templateUrl: 'partials/developers.html',
controller: 'DevelopersController'
})
.otherwise({
redirectTo: '/home'
});
@@ -21,7 +25,109 @@
/** DEVELOPERS **/
app.controller('DevelopersController', ['$scope', 'DevelopersService', function($scope, DevelopersService) {
function resetParams() {
$scope.actionName = "New Developer";
$scope.submitButtonLabel = "Insert";
$scope.currentDeveloper = {
id: null,
"name": null
};
$scope.isFormClean = true;
$scope.name = "";
}
function developerList() {
DevelopersService.list()
.then(function(response) {
$scope.developers = response.data;
});
}
function resetParamsAndLoadDeveloperList() {
resetParams();
developerList();
}
$scope.selectDeveloper = function(developer) {
$scope.actionName = "Update Developer";
$scope.submitButtonLabel = "Update";
$scope.currentDeveloper = developer;
$scope.name = $scope.currentDeveloper.name;
$scope.isFormClean = false;
};
$scope.submit = function() {
if ($scope.isFormClean) {
$scope.insert($scope.name);
} else {
$scope.currentDeveloper.name = $scope.name;
$scope.update($scope.currentDeveloper);
}
resetParamsAndLoadDeveloperList();
};
$scope.insert = function(name) {
DevelopersService.insert(name)
.then(function(response) {
resetParamsAndLoadDeveloperList();
});
};
$scope.update = function(developer) {
DevelopersService.update(developer)
.then(function(response) {
resetParamsAndLoadDeveloperList();
});
};
$scope.delete = function(developerId) {
DevelopersService.delete(developerId)
.then(function(response) {
resetParamsAndLoadDeveloperList();
});
};
$scope.cancel = function() {
resetParams();
};
resetParamsAndLoadDeveloperList();
}]);
app.service('DevelopersService', function($http) {
var service = this;
var path = '/developer/';
function getPath() {
return path;
}
function getUrl(action) {
return getPath() + action;
}
service.list = function() {
return $http.get(getUrl('list'));
};
service.insert = function(name) {
return $http.post(getUrl('create'), name);
};
service.update = function(developer) {
return $http.put(getUrl('update/' + developer.id), developer);
};
service.delete = function(id) {
return $http.delete(getUrl('delete/' + id), {
'id': id
});
};
});
/** BUGS **/
/** STORIES **/
@@ -0,0 +1,53 @@
<ol class="breadcrumb">
<li><a href="#/home"><i class="fa fa-home" aria-hidden="true"></i></a></li>
<li class="active">Developers</li>
</ol>
<div class="row" data-ng-controller="DevelopersController">
<div class="col-md-6">
<h4 class="text-center">Developers</h4>
<div class="white-background" style="padding:15px;">
<table class="table table-hover table-condensed">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="developer in developers" ng-class="(developer.id == currentDeveloper.id) ? 'warning' : ''">
<td></td>
<td width="80%"></td>
<td width="5%" style="text-align:center;">
<a class="btn btn-xs btn-success" ng-click="selectDeveloper(developer)"><i class="fa fa-pencil-square-o "></i></a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-1"></div>
<div class="col-md-5">
<h4 style="color:#B80D57;"></h4>
<div class="white-background" style="padding:20px;">
<form name="developerForm" ng-submit="submit()" method="POST">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="control-label" for="name">Name</label>
<input value="" name="name" id="name" type="text" ng-model="name" placeholder="" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<input class="btn btn-success btn-sm" type="submit" id="submit" value="" />
<a ng-if="currentDeveloper.id" ng-click="delete(currentDeveloper.id)" class="btn btn-danger btn-sm">Delete</a>
<a ng-if="currentDeveloper.id" ng-click="cancel()" class="btn btn-default btn-sm">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</div>

0 comments on commit 6fdb122

Please sign in to comment.