public void ChildSaved(Child c) { logger.Debug("Notification - new child added: " + c.ID); try { SendResponse(new NewChildResponse(DtoUtils.GetDto(c))); } catch (Exception e) { throw new Error(e.Message); } }
public Child SaveChild(Child c) { ChildDto childDto = DtoUtils.GetDto(c); SendRequest(new SaveChildRequest(childDto)); Response response = ReadResponse(); if (response is ErrorResponse) { ErrorResponse err = (ErrorResponse)response; throw new Error(err.Message); } SavedChildResponse savedChild = (SavedChildResponse)response; return(DtoUtils.GetFromDto(savedChild.Child)); }
private Response HandleRequest(Request request) { Response response = null; if (request is LoginRequest) { LoginRequest loginRequest = (LoginRequest)request; AdminDto dto = loginRequest.User; logger.Debug("Processing login request by user " + dto.ID); Admin user = DtoUtils.GetFromDto(dto); try { lock (server) { server.Login(user.ID, user.Password, this); } return(new OkResponse()); } catch (Error e) { logger.Debug("User " + dto.ID + " already logged in. Disconnecting..."); connected = false; return(new ErrorResponse(e.Message)); } } if (request is LogoutRequest) { LogoutRequest logoutRequest = (LogoutRequest)request; AdminDto dto = logoutRequest.User; logger.Debug("Processing logout request by user " + dto.ID); Admin user = DtoUtils.GetFromDto(dto); try { lock (server) { server.Logout(user.ID, this); } connected = false; return(new OkResponse()); } catch (Error e) { return(new ErrorResponse(e.Message)); } } if (request is SaveChildRequest) { SaveChildRequest saveChildRequest = (SaveChildRequest)request; ChildDto dto = saveChildRequest.Child; logger.Debug("Processing save request for child ID " + dto.ID); Child c = DtoUtils.GetFromDto(dto); try { Child result; lock (server) { result = server.SaveChild(c); } if (result != null) { return(new SavedChildResponse(DtoUtils.GetDto(result))); } else { return(new ErrorResponse("Child " + c.ID + " could not be saved")); } } catch (Error e) { return(new ErrorResponse(e.Message)); } } if (request is FilterChildrenRequest) { FilterChildrenRequest filterChildrenRequest = (FilterChildrenRequest)request; string data = filterChildrenRequest.Data; IList <int> parameters = Parser.ToList(data); logger.Debug("Processing filter children request for event ID " + parameters[0]); try { Child[] result; lock (server) { result = server.GetFilteredChildren(parameters[0], parameters[1], parameters[2]).ToArray(); } return(new ListOfChildrenResponse(DtoUtils.GetDto(result))); } catch (Error e) { return(new ErrorResponse(e.Message)); } } if (request is GetEventsRequest) { GetEventsRequest getEventsRequest = (GetEventsRequest)request; logger.Debug("Processing get events request"); try { Event[] events; lock (server) { events = server.GetAllEvents().ToArray(); } return(new ListOfEventsResponse(DtoUtils.GetDto(events))); } catch (Error e) { return(new ErrorResponse(e.Message)); } } if (request is CountChildrenRequest) { CountChildrenRequest countChildrenRequest = (CountChildrenRequest)request; logger.Debug("Processing count children request for event ID " + countChildrenRequest.EventID); try { int count; lock (server) { count = server.CountChildren(countChildrenRequest.EventID); } return(new CounterOfChildrenResponse(count)); } catch (Error e) { return(new ErrorResponse(e.Message)); } } return(response); }