Esempio n. 1
0
        public HttpResponseMessage Post([FromBody] DTO.Task task)
        {
            #region Preconditions

            if (userTaskRepository == null)
            {
                throw new InvalidOperationException();
            }

            if (task == null)
            {
                throw new ArgumentNullException();
            }

            #endregion

            var userTask = UserTaskMapper.TranslateDTOTaskToModelUserTask(task);

            int?newId = userTaskRepository.Add(userTask);

            if (newId == null)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }

            task.Id = newId.Value;

            var response = Request.CreateResponse <DTO.Task>(HttpStatusCode.Created, task);

            string uri = Url.Link("AddUserTask", new { id = task.Id });
            response.Headers.Location = new Uri(uri);
            return(response);
        }
Esempio n. 2
0
        public HttpResponseMessage Post([FromBody] DTO.Task task)
        {
            #region Preconditions

            if (userTaskRepository == null)
            {
                throw new InvalidOperationException();
            }

            if (task == null)
            {
                throw new ArgumentNullException();
            }

            #endregion

            try
            {
                var userTask = UserTaskMapper.TranslateDTOTaskToModelUserTask(task);

                int?newId = userTaskRepository.Add(userTask);

                if (newId == null)
                {
                    string taskDataError = JsonConvert.SerializeObject(task);
                    logger.Error($"TasksController Post userTaskRepository Add Task Failed Task Data: {taskDataError}", users.First(u => u.UserName == User.Identity.Name).Id);
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }

                task.Id = newId.Value;

                var response = Request.CreateResponse <DTO.Task>(HttpStatusCode.Created, task);

                string uri = Url.Link("AddUserTask", new { id = task.Id });
                response.Headers.Location = new Uri(uri);

                string taskData = JsonConvert.SerializeObject(task);
                logger.Info($"Task Create id: {task.Id}, taskData: {taskData}", users.First(u => u.UserName == User.Identity.Name).Id);

                return(response);
            }

            catch (Exception ex)
            {
                string taskData = JsonConvert.SerializeObject(task);
                logger.Error($"api/Tasks Post Error: {ex.Message}, taskData: {taskData}", users.First(u => u.UserName == User.Identity.Name).Id);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }