Beispiel #1
0
        public Task Post(Guid taskListId, Task task)
        {
            var userName = this.Request.GetUserPrincipal().Identity.Name;

            var listExists = this.model.TaskLists.Count(o => o.UserName == userName && o.Id == taskListId) == 1;
            if (!listExists)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            task.Id = Guid.NewGuid();
            task.UserName = userName;
            task.StartDate = DateTime.UtcNow;
            task.TimestampUpdate = DateTime.UtcNow;
            task.TaskListId = taskListId;

            this.model.Tasks.Add(task);
            this.model.SaveChanges();

            return task;
        }
Beispiel #2
0
        public Task Put(Guid taskListId, Task value)
        {
            var userName = this.Request.GetUserPrincipal().Identity.Name;

            var belongsToUser = this.model.Tasks.Count(o => o.Id == value.Id && o.TaskList.Id == taskListId && o.TaskList.UserName == userName) == 1;
            if (!belongsToUser)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            value.UserName = userName;
            value.TaskListId = taskListId;
            value.TimestampUpdate = DateTime.UtcNow;
            value.StartDate = value.StartDate != DateTime.MinValue ? value.StartDate : DateTime.UtcNow;
            value.DueDate = value.DueDate != DateTime.MinValue ? value.DueDate : null;

            this.model.Tasks.Attach(value);
            this.model.Entry<Task>(value).State = System.Data.EntityState.Modified;
            this.model.SaveChanges();

            return value;
        }