private TcpMessage HandleMultiRelationQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.MultiRelationQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbRelation>(TcpRequestType.List);

            if (request.HasParam("from_id"))
            {
                var fromId = request.GetParamAsNullableInt("from_id");
                var items  = _dataService.GetRelations(fromId);
                response.Add(items);
                return(response);
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
        private TcpMessage HandleInsertItem(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.InsertItem))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ObjectTcpMessage <DbItem>(message);

            var response = new ObjectTcpMessage <DbItem>(TcpRequestType.List);
            var item     = request.Item;

            if (item != null)
            {
                _dataService.Insert(item);
                response.Add(item); // Send the item back to report assigned Id
            }

            return(response);
        }
        private TcpMessage HandleUniqueItemQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.UniqueItemQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbItem>(TcpRequestType.List);

            if (request.HasParam("id"))
            {
                var id = request.GetParamAsNullableInt("id");
                if (id != null)
                {
                    var item = _dataService.GetItem(id.Value);
                    response.Add(item);
                    return(response);
                }
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
        private TcpMessage HandleDeleteItem(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.DeleteItem))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ObjectTcpMessage <DbItem>(message);

            var item = request.Item;

            if (item != null)
            {
                _dataService.Delete(item);
            }

            return(new TcpMessage(TcpRequestType.Ok));
        }