Exemple #1
0
        public void Put(int id, DTO.Task task)
        {
            #region Preconditions

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

            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

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

            #endregion

            UserTask userTask = null;

            task.Id  = id;
            userTask = UserTaskMapper.TranslateDTOTaskToModelUserTask(task);

            if (!userTaskRepository.Update(userTask))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
Exemple #2
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);
        }
Exemple #3
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));
            }
        }
Exemple #4
0
        public HttpResponseMessage Put(int id, DTO.Task task)
        {
            #region Preconditions

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

            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

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

            #endregion

            try
            {
                UserTask userTask = null;

                task.Id  = id;
                userTask = UserTaskMapper.TranslateDTOTaskToModelUserTask(task);

                if (!userTaskRepository.Update(userTask))
                {
                    string taskDataError = JsonConvert.SerializeObject(task);
                    logger.Error($"TasksController Put userTaskRepository Failed id: {id}, Task Data: {taskDataError}", users.First(u => u.UserName == User.Identity.Name).Id);
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                try
                {
                    new AppServices.NotificationsService().NotifyUsers(userTask);
                }
                catch (Exception ex)
                {
                    string taskDataError = JsonConvert.SerializeObject(task);
                    logger.Error($"TasksController Put userTaskRepository NotifyUsers Failed id: {id}, Message: {ex.Message}, taskData: {taskDataError}", users.First(u => u.UserName == User.Identity.Name).Id);
                }

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

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

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

                return(response);
            }

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