public static Reservation mapToJSON(booking item) { Reservation json = new Reservation(); json.id = item.id; json.branch_fk = item.branch_fk; json.room_fk = item.room_fk; json.responsible = item.responsable; json.startdate = item.startDate; json.starttime = (item.startDate.Hour * 60) + item.startDate.Minute; // Conversao necessaria já que as horas sao usadas em minutos no client-side. json.endtime = (item.endDate.Hour * 60) + item.endDate.Minute; json.enddate = item.endDate; json.description = item.description; if (item.coffee != null) json.coffee = (int)item.coffee; return json; }
public JsonResult Save(Reservation json) { try { // Valida as anotations dos attributos do json de entrada (se é required, maxlenght e etc) if (!ModelState.IsValid) { List<ValidationError> answerValidationErrors = GetJSONValidationErrorsList(); return Json(new Answer(400, "Validation Error!", answerValidationErrors), JsonRequestBehavior.AllowGet); } // Transforma o objeto json em um objeto do modelo: booking reservation = Reservation.map(json); // Verifica se a sala já está reservada naquele período if (!IsRoomFree(reservation)) { return Json(new Answer(400, "Esta sala já está reservada neste horário!"), JsonRequestBehavior.AllowGet); } if (reservation.id > 0) // Atualiza a reserva da sala no banco de dados: UpdateReservation(reservation); else // Efetua a reserva da sala no banco de dados: SaveReservation(reservation); return Json(new Answer(200, "OK")); } catch (DbEntityValidationException dbEx) { List<ValidationError> answerValidationErrors = GetModelValidationErrorsList(dbEx); return Json(new Answer(400, "Validation Error!", answerValidationErrors), JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new Answer(500, "Something went wrong: " + ex.Message), JsonRequestBehavior.AllowGet); } }
internal static booking map(Reservation json) { booking b = new booking(); if (json.id != null && json.id > 0) { b.id = json.id; } b.branch_fk = json.branch_fk; b.room_fk = json.room_fk; b.responsable = json.responsible; // Calcula o startDate (tem que pegar o dia do json.startdate e as horas do json.starttime e converter esses 2 valores em um unico DateTime) DateTime sd = json.startdate; TimeSpan tsSd = new TimeSpan(Convert.ToInt32(json.starttime / 60), Convert.ToInt32(json.starttime % 60), 0); sd = sd.Date + tsSd; // Calcula o endDate DateTime ed = json.enddate; TimeSpan tsEd = new TimeSpan(Convert.ToInt32(json.endtime / 60), Convert.ToInt32(json.endtime % 60), 0); sd = sd.Date + tsEd; b.startDate = sd; b.endDate = ed; b.description = json.description; if (json.coffee != null && json.coffee > 0) b.coffee = json.coffee; return b; }