public static void waitForUserInput() { var newLine = Console.ReadLine(); switch (newLine) { case "exit": { killProcess = true; Environment.Exit(0); break; } case "test": { InternalEvent.getAll().ContinueWith((obj) => { var res = obj.Result; }); break; } case "get": { Console.WriteLine(EventREST.GetEvents()); break; } } }
public EventREST() { Get[MainClass.LoftPrefix + "events"] = (dynamic arg) => { return(GetEvents()); }; Get[MainClass.LoftPrefix + "event/{guid:guid}", runAsync : true] = async(dynamic parameters, System.Threading.CancellationToken arg2) => { var eventoGuid = (Guid)parameters.guid; var evento = (await InternalEvent.GetOne(eventoGuid)).items.FirstOrDefault(); if (evento == null) { return(HttpStatusCode.NotFound); } return(JsonConvert.SerializeObject(evento)); }; Get[MainClass.LoftPrefix + "event/{guid:guid}/accept", runAsync : true] = async(dynamic parameters, System.Threading.CancellationToken arg2) => { var eventoGuid = (Guid)parameters.guid; var evento = (await InternalEvent.GetOne(eventoGuid)).items.FirstOrDefault(); if (evento == null) { return(HttpStatusCode.NotFound); } evento.data.State = InternalEvent.PersonalizedData.EventStateEnum.Approved; var resUpdate = await evento.update(); Event googleEvent = null; User utente = null; try { googleEvent = await Generic.Calendar.Events.Get(MainClass.StudentsCalendar, evento.data.RelatedGoogleEventID).ExecuteAsync(); utente = (await User.GetOne(evento.data.RelatedOwnerGuid)).items.FirstOrDefault(); } catch { } //TODO:manda la mail all'utente if (utente != null && googleEvent != null) { await SendMail("Prenotazione evento LOFT", "La tua prenotazione per " + (googleEvent?.End?.DateTime ?? DateTime.MinValue).ToString("f") + "è stata ACCETTATA. " + "Rispondi a questa email per chiedere informazioni", MainClass.StaffMail, utente.data.Email ); } if (resUpdate.state == WebServiceV2.WebRequestState.Ok) { return("Evento ACCETTATO"); } else { return(resUpdate.errorMessage); } }; Get[MainClass.LoftPrefix + "event/{guid:guid}/deny", runAsync : true] = async(dynamic parameters, System.Threading.CancellationToken arg2) => { var eventoGuid = (Guid)parameters.guid; var evento = (await InternalEvent.GetOne(eventoGuid)).items.FirstOrDefault(); if (evento == null) { return(HttpStatusCode.NotFound); } evento.data.State = InternalEvent.PersonalizedData.EventStateEnum.Rejected; var resUpdate = await evento.update(); //ora cancellalo anche da Google Calendar Event googleEvent = null; User utente = null; try { googleEvent = await Generic.Calendar.Events.Get(MainClass.StudentsCalendar, evento.data.RelatedGoogleEventID).ExecuteAsync(); utente = (await User.GetOne(evento.data.RelatedOwnerGuid)).items.FirstOrDefault(); } catch { } var request = Generic.Calendar.Events.Delete( MainClass.StudentsCalendar, evento.data.RelatedGoogleEventID ); await request.ExecuteAsync(); //TODO:manda la mail all'utente if (utente != null && googleEvent != null) { await SendMail("Prenotazione evento LOFT", "La tua prenotazione per il " + (googleEvent?.End?.DateTime ?? DateTime.MinValue).ToString("f") + "è stata RIFIUTATA." + "Rispondi a questa email per chiedere informazioni", MainClass.StaffMail, utente.data.Email ); } if (resUpdate.state == WebServiceV2.WebRequestState.Ok) { return("Evento RIFIUTATO ed eliminato da Google Calendar"); } else { return(resUpdate.errorMessage); } }; Get[MainClass.LoftPrefix + "file/get/{fileid}"] = (dynamic arg) => { string fileID = arg.fileid; string dirPath = "./cache/"; string filePath = dirPath + fileID; string ContentType = "image/jpg"; var memory = new MemoryStream(); if (Directory.Exists(dirPath) == false) { Directory.CreateDirectory(dirPath); } //verifica se esiste in cache if (File.Exists(filePath)) { using (var fileStream = File.OpenRead("./cache/" + fileID)) { fileStream.CopyTo(memory); } } else { var req = Generic.Drive.Files.Get(fileID); var res = req.Execute(); req.Download(memory); memory.Position = 0; //salva lo stream in cache using (var fileStream = File.Create("./cache/" + fileID)) { memory.CopyTo(fileStream); } } memory.Position = 0; return(Response.FromStream(memory, ContentType)); }; Post[MainClass.LoftPrefix + "event/{id}/update", runAsync : true] = async(dynamic arg1, System.Threading.CancellationToken arg2) => { string eventID = arg1.id; var inputStr = Request.Body.AsString(); var h = JsonConvert.DeserializeObject <GoogleEventUpdateRequest>(inputStr); var i = h.ObjGoogleEvent; if (string.IsNullOrWhiteSpace(eventID) == false) { i.ID = eventID; } return(await UpdateEvent(i, h.RelatedOwnerGuid)); }; }