Example #1
0
        public void UpdateTodo(string todoId, TodoType todo)
        {
            try
            {
                #region Validate XML message

                if (!IsValidMessage())
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.XsdValidationFailed);
                    return;
                }

                #endregion

                int todoIdAsInt;
                if( !int.TryParse(todoId, out todoIdAsInt))
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.InvalidIdSpecified);
                    return;
                }

                if (!todo.idSpecified || todo.id != todoIdAsInt)
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.InvalidIdSpecified);
                    return;
                }

                var existing = _todos.First(td => td.id == todoIdAsInt);
                _todos.Remove(existing);

                _todos.Add(todo);

                // Return success
                SetHttpStatus(HttpStatusCode.OK, HttpHeaderMessages.TodoUpdatedOrCreated);
            }
            catch (Exception exception)
            {
                _log.ErrorFormat("Error processing CreateTodo : {0}", exception);
                SetHttpStatus(HttpStatusCode.InternalServerError, HttpHeaderMessages.InternalServerError);
            }
        }
Example #2
0
        public int CreateTodo(TodoType todo)
        {
            try
            {
                #region Validate XML message

                if (!IsValidMessage())
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.XsdValidationFailed);
                    return 0;
                }

                #endregion

                if (todo.idSpecified)
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.IdSpecified);
                    return 0;
                }

                var newId = _todos.Max(td => td.id) + 1;

                todo.id = newId;
                _todos.Add(todo);

                // Return success
                SetHttpStatus(HttpStatusCode.OK, HttpHeaderMessages.TodoUpdatedOrCreated);

                return newId;
            }
            catch (Exception exception)
            {
                _log.ErrorFormat("Error processing CreateTodo : {0}", exception);
                SetHttpStatus(HttpStatusCode.InternalServerError, HttpHeaderMessages.InternalServerError);
                return 0;
            }
        }
Example #3
0
        public void UpdateTodo()
        {
            using (var server = new WebServiceHost(new TodoService(), new Uri(_hostAddress)))
            using (var client = new WebChannelFactory<ITodoApi>(new WebHttpBinding(), new Uri(_hostAddress)))
            {
                server.Open();
                client.Open();
                var channel = client.CreateChannel();

                var todo = new TodoType();

                using (new OperationContextScope((IContextChannel)channel))
                {
                    try
                    {
                        todo = channel.GetTodo("1");
                    }
                    catch (CommunicationException ex)
                    {
                        ValidateHttpStatusResponse(ex, HttpStatusCode.BadRequest);
                    }
                }

                todo.IsDone = true;

                using (new OperationContextScope((IContextChannel) channel))
                {
                    channel.UpdateTodo(todo.id.ToString(), todo);
                    ValidateHttpStatusResponse(HttpStatusCode.OK);
                }
            }
        }
Example #4
0
        public void CreateTodo()
        {
            using (var server = new WebServiceHost(new TodoService(), new Uri(_hostAddress)))
            using (var client = new WebChannelFactory<ITodoApi>(new WebHttpBinding(), new Uri(_hostAddress)))
            {
                server.Open();
                client.Open();
                var channel = client.CreateChannel();

                var todo = new TodoType { id = 6, idSpecified = true, IsDone = false, Name = "New Item", Description = "This items should be fixed!", ExpirationDate = DateTime.Now.AddDays(2) };

                using (new OperationContextScope((IContextChannel)channel))
                {
                    try
                    {
                        var newId = channel.CreateTodo(todo);
                    }
                    catch (CommunicationException ex)
                    {
                        ValidateHttpStatusResponse(ex, HttpStatusCode.BadRequest);  
                    }
                }

                todo.idSpecified = false;

                using (new OperationContextScope((IContextChannel)channel))
                {
                    var newId = channel.CreateTodo(todo);
                    ValidateHttpStatusResponse(HttpStatusCode.OK);

                    Assert.AreEqual(4, newId);
                }
            }
        }
Example #5
0
        public void UpdateTodo(string todoId, TodoType todo)
        {
            try
            {
                message.Text = "UpdateTodo";

                int todoIdAsInt;
                if( !int.TryParse(todoId, out todoIdAsInt))
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.InvalidIdSpecified);
                    return;
                }

                if (!todo.idSpecified || todo.id != todoIdAsInt)
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.InvalidIdSpecified);
                    return;
                }

                var existing = _todos.First(td => td.id == todoIdAsInt);
                _todos.Remove(existing);

                _todos.Add(todo);

                // Return success
                SetHttpStatus(HttpStatusCode.OK, HttpHeaderMessages.TodoUpdatedOrCreated);
            }
            catch 
            {
                SetHttpStatus(HttpStatusCode.InternalServerError, HttpHeaderMessages.InternalServerError);
            }
        }
Example #6
0
        public int CreateTodo(TodoType todo)
        {
            try
            {
                message.Text = "CreateTodo";

                if (todo.idSpecified)
                {
                    SetHttpStatus(HttpStatusCode.BadRequest, HttpHeaderMessages.IdSpecified);
                    return 0;
                }

                var newId = _todos.Max(td => td.id) + 1;

                todo.id = newId;
                _todos.Add(todo);

                // Return success
                SetHttpStatus(HttpStatusCode.OK, HttpHeaderMessages.TodoUpdatedOrCreated);

                return newId;
            }
            catch
            {
                SetHttpStatus(HttpStatusCode.InternalServerError, HttpHeaderMessages.InternalServerError);
                return 0;
            }
        }