Skip to content

Commit

Permalink
DtaService added
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilshastri committed Jul 24, 2016
1 parent 51dd6ba commit 2b09a25
Show file tree
Hide file tree
Showing 23 changed files with 281 additions and 87 deletions.
18 changes: 18 additions & 0 deletions app/common/appErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,22 @@ export class InvalidArgumentTypeException extends Error{
super();
this.message = " Dependencies lengths not per param length ";
}
}
export class InvalidEndpointsException extends Error{
constructor(){
super();
this.message = "Endpoints not defined";
}
}
export class LocalStorageNotSupportedException extends Error{
constructor(){
super();
this.message = "Local Storage is not supported";
}
}
export class InvalidJSONException extends Error{
constructor(){
super();
this.message = "JSON format is not supported";
}
}
5 changes: 5 additions & 0 deletions app/components/sketchListItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class SketchListItem {
constructor() {
this.replace = true;
this.template = template;
this.scope = {
author:"=",
title:"=",
id:"="
};
}

static directiveFactory() {
Expand Down
16 changes: 12 additions & 4 deletions app/components/sketchListItem/view.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-2 col-lg-2 ">
<div class="box tmp">Sketch-icon</div>
<div class="box tmp">
{{title}}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8 ">
<div class="box tmp">Details</div>
<div class="box tmp">
{{author}}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-2 col-lg-2 ">
<div class="box tmp">
<a class="" href="#">
<a class="" ng-href="#/view/{{id}}">
View
</a>
&nbsp;
&nbsp;<a class="" ng-href="#/edit/{{id}}">
Edit
</a>
&nbsp;
<a class="" href="#">
<a class="" ng-href="#/delete/{{id}}">
Del
</a>
</div>
Expand Down
24 changes: 24 additions & 0 deletions app/components/toolBox/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import template from '!html!./view.html';
import {directive, inject} from 'ng-app';
import './style.less';

@directive({selector: 'tool-box'})
class ToolBox {

constructor() {
this.replace = true;
this.template = template;
this.scope={
tools:"="
};
this.link = this.link.bind(this);
}

link(scope,el,attr){

}

static directiveFactory() {
return new ToolBox();
}
}
3 changes: 3 additions & 0 deletions app/components/toolBox/style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.header {
background-color: #6699cc;
}
3 changes: 3 additions & 0 deletions app/components/toolBox/view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ul ng-repeat="tool in tools">
<li data-id="{{tool.id}}" data-attrs="{{tool.attr}}"> {{tool.name}}</li>
</ul>
16 changes: 5 additions & 11 deletions app/configs.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import {config, inject} from './ng-app';
import sketches from './views/sketches';
import edit from './views/edit';


class ConfigClass{
class ApplicationConfig{

@inject('$stateProvider', '$urlRouterProvider')
@config()
Routing(sp,urp){

// sp.state('sketchs', {
// url:'/sketchs',
// templateUrl: "views/sketches/view.html"
// // template: '<h1>My Contacts</h1>'
// }) ;

sp.state('sketchs', sketches) ;
urp.otherwise('/sketchs');
sp.state('sketches', sketches) ;
sp.state('edit', edit) ;
urp.otherwise('/sketches');
}


}
3 changes: 3 additions & 0 deletions app/css/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
}
}

.page {
min-height: 250px;
}
.tmp {
background-color:#c94a4a;
}
Expand Down
2 changes: 1 addition & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'components/header';
import 'components/footer';
import 'configs';
import 'services';

import 'models';


import './utils/animation-patch'
1 change: 1 addition & 0 deletions app/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './sketchModel';
2 changes: 0 additions & 2 deletions app/models/sketchModel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
* Created by Akhil on 24-07-2016.
*/
import {model} from './model'
import {InvalidArgumentTypeException} from 'common/appErrors';

@model({endpoint:'sketch'})
export class SketchModel {

constructor(sketch){
Expand Down
70 changes: 70 additions & 0 deletions app/services/baseService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Created by Akhil on 24-07-2016.
*/
import {InvalidEndpointsException} from 'common/appErrors';

export default class BaseDataService {

getAll() {
const srvc = this.getEndPointService();
const method = this.methods.GET;
return srvc[method](this.buildGetAllURL());
}

getModel(id) {
const srvc = this.getEndPointService();
const method = this.methods.GET;
return srvc[method](this.buildGetURL(id));
}

insert(model) {
const srvc = this.getEndPointService();
const method = this.methods.PUT;
return srvc[method](this.buildInsertURL(),model);
}

update(id, newModel) {
const srvc = this.getEndPointService();
const method = this.methods.POST;
return srvc[method](this.buildUpdateURL(),newModel);
}

delete(id) {
const srvc = this.getEndPointService();
const method = this.methods.DELETE;
return srvc[method](this.buildDeleteURL());
}

buildGetAllURL() {
return `${this.getEndPointURL()}`;
}

buildGetURL(id) {
return `${this.getEndPointURL()}/${id}`;

}

getEndPointURL() {
if (!this.endpoint) {
throw new InvalidEndpointsException();
}
return this.endpoint;
}

buildInsertURL() {
return `${this.getEndPointURL()}`;

}

buildUpdateURL() {
return `${this.getEndPointURL()}`;
}

buildDeleteURL() {
return `${this.getEndPointURL()}`;
}

getEndPointService(){
throw new Error('Method not implemented');
}
}
25 changes: 13 additions & 12 deletions app/services/dataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
*/

import {service, inject} from 'ng-app';
import Base from 'common/Base';
import {MOCK_HTTP} from 'constants';

@service({name:'DataService'})
@inject('$http')
class DataService extends Base {
constructor() {
super(arguments);
debugger;
@service({name: 'DataService'})
@inject('$http', 'LocalDataService')
class DataServiceAdaptor {
constructor(http, ld) {
this.http = http;
this.ld = ld;
}

get mapDI(){
return ['$h'];
getEndPointService(){
if(!MOCK_HTTP){
throw new Error ('$http implementation is not supported');
}
return this.ld;
}
}

get http(){

}
}
4 changes: 2 additions & 2 deletions app/models/model.js → app/services/decorator/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Created by Akhil on 24-07-2016.
*/

export function model(param) {
export function rest(param) {
return function (target, name, decor) {
Object.assign(target.prototype,
{
endpoint: target.name,
methods: {get: 'get', put: 'put', post: 'post', 'delete': 'delete'}
methods: {GET: 'get', PUT: 'put', POST: 'post', 'DELETE': 'delete'}
}
, param)
}
Expand Down
6 changes: 5 additions & 1 deletion app/services/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/**
* Created by Akhil on 24-07-2016.
*/
import './dataService';
import './baseService';
import './dataService';
import './localDataService';
import './sketchDataService';
import './decorator/rest';
23 changes: 0 additions & 23 deletions app/services/localDataSerivce.js

This file was deleted.

38 changes: 38 additions & 0 deletions app/services/localDataService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Created by Akhil on 24-07-2016.
*/
import {LocalStorageNotSupportedException, InvalidJSONException} from 'common/appErrors';
import {service} from 'ng-app';

@service({name: 'LocalDataService'})
class LocalDataService {
constructor() {
if (!window || !window.localStorage) {
throw new LocalStorageNotSupportedException();
}
}

get(url) {
const promise = new Promise(
(resolve, reject) => {
const [key,id] = url.split('/');
var data = this.storage.getItem(key) || '[]';
try {
data = JSON.parse(data);
} catch (e) {
reject(new InvalidJSONException(e));
}
resolve(
( typeof id === 'undefined') ? data : data.find((x)=>x.id == id)
);

}
);
return promise;
}

get storage() {
return window.localStorage;
}

}
20 changes: 9 additions & 11 deletions app/services/sketchDataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
*/

import {service, inject} from 'ng-app';
import Base from 'common/Base';
import BaseDataService from './baseService';
import {rest} from './decorator/rest';

@service({name:'SketchDataService'})
@inject('DataService')
class SketchDataService extends Base {
constructor() {
@rest({endpoint:'sketch'})
@service({name:'SketchDataService'})
class SketchDataService extends BaseDataService {
constructor(ds) {
super(arguments);
debugger;
}
get mapDI(){
return ['$h'];
this.ds= ds;
}


get http(){

getEndPointService(){
return this.ds.getEndPointService() ;
}
}
File renamed without changes.
Loading

0 comments on commit 2b09a25

Please sign in to comment.