Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Integracão reserva api #9

Merged
merged 11 commits into from
Jul 19, 2017
32 changes: 2 additions & 30 deletions client/src/main/js/controller/CalendarioController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,10 @@
* @return {Promise} Promise do modal de visualização da reserva
*/
this.clickReserva = function(reserva) {
let reservaOriginal = angular.copy(reserva);

return ModalService.verReserva(reserva).then(function () {
if(reservaFoiAlterada(reserva, reservaOriginal)) {
atualizaReserva(reserva);
}
});
const reservaObj = new Reserva(reserva);
return ModalService.verReserva(reservaObj);
};

/**
* Atualiza o horário de início e fim da reserva especificada, utilizando o model
* Reserva, para buscar os valores em Date.
*
* @param reserva Reserva a ter início e fim atualizados.
*/
function atualizaReserva(reserva) {
const reservaModelo = new Reserva(reserva);

reserva.start = reservaModelo.start;
reserva.end = reservaModelo.end;
}

/**
* Retorna se uma reserva foi alterada.
*
* @param reserva Reserva que pode ter sido alterada.
* @param reservaOriginal Reserva antes da possível alteração
*/
function reservaFoiAlterada(reserva, reservaOriginal) {
return JSON.stringify(new Reserva(reserva)) !== JSON.stringify(new Reserva(reservaOriginal));
}

/**
* Callback executado quando o usuário clica em um determinado dia.
* Temos que decidir qual será o comportamento executado, pois podemos manter o usuário
Expand Down
25 changes: 12 additions & 13 deletions client/src/main/js/controller/DetalhesReservaController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Controller responsável pelo modal de detalhes da reserva.
*
*/
angular.module('calendarioModulo').controller('DetalhesReservaController', ['reserva', '$mdDialog', 'ModalService', 'AuthService', 'AgendamentoService', function (reserva, $mdDialog, ModalService, AuthService, AgendamentoService) {
angular.module('calendarioModulo').controller('DetalhesReservaController', ['reserva', '$mdDialog', 'ModalService', 'AuthService', 'AgendamentoService', 'Reserva', 'ToastService', function (reserva, $mdDialog, ModalService, AuthService, AgendamentoService, Reserva, ToastService) {

const self = this;

Expand All @@ -29,7 +29,7 @@
*/
function preEdicao() {
self.reservaOriginal = self.reserva;
self.reserva = angular.copy(self.reserva);
self.reserva = new Reserva(angular.copy(self.reserva));
ajustarInicioFim();
}

Expand Down Expand Up @@ -80,14 +80,13 @@
self.reserva.autor = undefined;
return ModalService.verReserva(self.reserva);
};

return AgendamentoService.salvarReserva(self.reserva)
.then(data => {
posSalvar(data);
return self.reserva.salvar().then(data => {
AgendamentoService.salvarReserva(self.reserva);
ToastService.showActionToast("Reserva atualizada.");
posSalvar();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toaster para notificar sucesso

return data;
}, err => {
return ModalService.error(err.mensagem)
.then(callbackReabrirReserva);
return ModalService.error(err.data).then(callbackReabrirReserva);
});
};

Expand All @@ -102,11 +101,9 @@
/**
* Operações que devem ser executadas após o salvamento de agentamento.
* -Copiar os dados do agendamento para o antigo (necessário apenas para não precisar recarregar o dia)
* @param {*} data Dados retornados após salvamento.
*/
function posSalvar(data) {
angular.copy(data, self.reservaOriginal);
self.fecharModal();
function posSalvar() {
Object.assign(self.reservaOriginal, self.reserva);
}

/**
Expand Down Expand Up @@ -134,8 +131,10 @@
* @return Promise.
*/
this.excluirReserva = () => {
return AgendamentoService.excluir(self.reserva).then(data => {
return self.reserva.excluir().then(data => {
AgendamentoService.excluir(self.reserva);
self.fecharModal();
ToastService.showActionToast("Reserva excluída.");
return data;
});
};
Expand Down
46 changes: 37 additions & 9 deletions client/src/main/js/factory/Reserva.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ let sequenceReserva = 1;
/**
* Factory que representa a entidade de reserva.
*/
angular.module('agendamentoModulo', []).factory('Reserva', ['$http', '$q', function ($http, $q) {
angular.module('agendamentoModulo', []).factory('Reserva', ['$http', function ($http) {

const COR_DEFAULT = '', COR_TEXTO_DEFAULT = '',
DIA_INDICE = 0, MES_INDICE = 1, ANO_INDICE = 2,
HORA_INDICE = 0, MINUTO_INDICE = 1;

const API = "/api/reservas";

/*
* Obs: Provavelmente terá que ser adicionado duas funções privadas para criação de
* uma reserva, uma para quando nós iremos montar e outra para quando receber o evento
Expand All @@ -18,7 +20,7 @@ let sequenceReserva = 1;
*
* Todo: Remover id, o qual será gerado pelo mongodb
*/
function Reserva({id = sequenceReserva++,
function Reserva({_id,
autor,
titulo,
descricao,
Expand All @@ -28,7 +30,7 @@ let sequenceReserva = 1;
cor = COR_DEFAULT,
corTexto = COR_TEXTO_DEFAULT}) {

Object.assign(this, {id,
obterPropriedades(this, {_id,
autor,
titulo,
descricao,
Expand All @@ -39,38 +41,64 @@ let sequenceReserva = 1;
corTexto});
}

/**
* Define todas as propriedades da origem para
* o destino. Shallow copy.
*
* @param {*} instancia Objeto de destino.
* @param {*} props Objeto de origem das propriedades.
*/
function obterPropriedades(instancia, props) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSDoc

Object.assign(instancia, props);
}

/**
* Recupera a reserva original do servidor.
* @return Promise da requisição.
*/
Reserva.prototype.carregar = function () {
return $q.when({});
return $http.get(`${API}/${this._id}`).then(data => {
obterPropriedades(this, data.data);
return data;
});
};

/**
* Persiste a reserva.
* @return Promise da requisição.
*/
Reserva.prototype.salvar = function () {
return $q.when(this);
if (this._id) {
return this.atualizar();
}
return $http.post(API, this).then(data => {
obterPropriedades(this, data.data);
return data;
});
};

/**
* Exclui a reserva do servidor.
* @return Promise da requisição.
*/
Reserva.prototype.excluir = function () {
delete this.autor;
delete this.descricao;
return this.salvar();
return $http.delete(`${API}/${this._id}`).then(data => {
delete this.autor;
delete this.descricao;
delete this.titulo;
return data;
});
};

/**
* Atualiza a requisição no servidor.
* @return Promise da requisição.
*/
Reserva.prototype.atualizar = function () {
return $q.when({});
return $http.patch(`${API}/${this._id}`, this).then(data => {
obterPropriedades(this, data.data);
return data;
});
};

/**
Expand Down
Loading