pranayaryal
8/17/2017 - 7:56 PM

Angular Service example

Angular Service example

angular
    .module('sarApp')
    .service('CommentService', function($http) {
        var self = this;

        var comment = {
            
                body: '',
                type_id: '',
                follow_up_date: '',
                follow_up_link: '',
                completed: '',
                completed_date: '',
                reply_message: ''
                
            
        };


        
        // var payload = {
        //     object_id: object_id,
        //     object_type_id: object_type_id,
        //     comment: ''
        // };

        var response = '';

        this.getComment = function() {
            return comment;
        };

        this.setComment = function(comment) {
            this.comment = comment;
        };
        
        this.getPayload = function(payload) {
            return payload;
        };

        this.setPayload = function (payload) {
            this.payload = payload;
        };


        this.getComments = function(callback) {
            
            $http.post('/api/v1/comments/get_comments', this.payload)
                .success(function (data, status, headers, config) {
                    callback(data);
                }).
                error(function (data, status, header, config) {
                console.log('oops there was an error');
                callback(data);
            })
        };
        
        this.saveComment = function () {
            
        };
        
        this.removeComment = function (callback) {
            $http.post('/api/v1/comments/delete_comment', this.payload)
                .success(function(data) {
                   callback(data);
                }).
                error(function (data, status, header, config) {
                console.log('oops there was an error');
                callback(data);
            })
        };
        
        this.storeComment = function(callback) {
            $http.post('/api/v1/comments/save_comment', this.payload)
                .success(function(data) {
                callback(data);

            }).
                error(function (data, status, header, config) {
                callback(data);
            })
        };

        this.emailNssarAcnExists = function () {
            return $http.post('/api/v1/donor/emailNssarAcn', this.payload )
                .then(function (result) {
                    return result.data;
                })
        };

        this.setUser = function(user_id){
            this.user_id = user_id;
        }

        this.getTheUser = function(callback){
            
            $http.get('/api/v1/users/get/' + this.user_id)
                .success(function(data) {
                   callback(data);
                }).
                error(function (data, status, header, config) {
                console.log('oops there was an error');
                callback(data);
            });
        };

        this.checkDonorDetails = function(donor){
            var valid = true;
            var errors = {
                first_name: false,
                last_name: false,
                phone_number: false,
                address : false,
                city: false,
                email: false,
                postal_code: false
            };
            this.errors = errors;
            if (donor.contact.first_name === null || donor.contact.first_name.length === 0) {
                this.errors.first_name = true;
                valid = false;
            }

            if (donor.contact.last_name === null || donor.contact.last_name.length === 0) {
                this.errors.last_name = true;
                valid = false;
            }


            if (donor.contact.phone_number === null || donor.contact.phone_number.length === 0) {
                this.errors.phone_number = true;
                
                valid = false;
            }

            if (donor.contact.address === null || donor.contact.address.length === 0) {
                this.errors.address = true;
                valid = false;
            }


            if (donor.contact.city === null || donor.contact.city.length === 0) {
                this.errors.city = true;
                valid = false;
            }

            if (donor.contact.state == 0) {
                this.errors.state = true;
                valid = false;
            }

            if (donor.contact.email === null || donor.contact.email.length === 0) {
                this.errors.email = true;
            }

            if (donor.contact.postal_code === null || donor.contact.postal_code.length === 0) {
                this.errors.postal_code = true;
                valid = false;
            }

            return valid;

        };

        this.getDonorErrors = function () {
            return this.errors;
        }
        
        
        

    });