示例#1
0
        protected override Task <AddCategoryResponse> ProcessCommand(AddCategoryCommand command)
        {
            var response = new AddCategoryResponse();
            var request  = command.Data;
            var user     = _context.AcUsers.FirstOrDefault(e => e.UserName == request.UserName);

            var category = new AcCategory
            {
                CategoryName = request.Category,
                User         = user,
                IsDeleted    = false
            };

            _context.AcCategories.Add(category);
            var result = _context.SaveChanges();

            if (result > 0)
            {
                response.Category = _mapper.Map <Category>(category);
            }
            else
            {
                throw new Exception("An error occur while adding category.");
            }


            return(Task.FromResult(response));
        }
        protected override Task <UpdateCategoryResponse> ProcessCommand(UpdateCategoryCommand command)
        {
            var response = new UpdateCategoryResponse();
            var request  = command.Data;
            var user     = _context.AcUsers.FirstOrDefault(e => e.UserName == request.UserName);
            var category = _context.AcCategories.FirstOrDefault(e => e.CategoryId == request.CategoryId);

            if (category != null)
            {
                category.CategoryName = request.Name;
                _context.Attach(category);
                var result = _context.SaveChanges();

                if (result == 0)
                {
                    throw new Exception("An error occured while updating category");
                }
                else
                {
                    response.Category = _mapper.Map <Category>(category);
                }
            }

            return(Task.FromResult(response));
        }
示例#3
0
        protected override Task <UpdateTodoResponse> ProcessCommand(UpdateTodoCommand command)
        {
            var response = new UpdateTodoResponse();
            var request  = command.Data;
            var user     = _context.AcUsers.FirstOrDefault(e => e.UserName == request.UserName);
            var task     = _context.AcTasks.FirstOrDefault(e => e.TaskId == request.TaskId);

            if (task != null)
            {
                var category = _context.AcCategories.FirstOrDefault(e => e.UserId == user.UserId && e.CategoryId == request.CategoryId && e.IsDeleted == false);

                if (category != null)
                {
                    task.Name           = request.Name;
                    task.Category       = category;
                    task.RootTaskId     = request.RootTaskId == 0 ? null : request.RootTaskId;
                    task.Status         = (short)request.TaskStatus;
                    task.TaskPriorityId = (short)request.TaskPriority;

                    using (var scope = _context.Database.BeginTransaction())
                    {
                        try
                        {
                            _context.AcTasks.Attach(task);
                            var activity = new AcTaskActivity
                            {
                                Activity     = "Task Updated",
                                ActivityDate = DateTime.Now,
                                Task         = task
                            };
                            _context.Add(activity);
                            _context.SaveChanges();
                            response.Todo = _mapper.Map <Todo>(task);
                            scope.Commit();
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            throw new Exception("An error occured while creating task.");
                        }
                    }
                }
                else
                {
                    throw new Exception("An error occured while creating task.");
                }
            }
            else
            {
                throw new Exception("An error occured while creating task.");
            }

            return(Task.FromResult(response));
        }
        protected override Task <RegisterUserResponse> ProcessCommand(RegisterUserCommand command)
        {
            var response = new RegisterUserResponse();
            var entity   = _mapper.Map <AcUser>(command.Data);

            entity.CreateDate = DateTime.Now;
            var user = _context.AcUsers.Where(e => e.UserName == entity.UserName);

            if (user.Count() > 0)
            {
                throw new Exception("UserName is already in use");
            }
            var email = _context.AcUsers.Where(e => e.Email == entity.Email);

            if (email.Count() > 0)
            {
                throw new Exception("Email is already in use");
            }

            var security = new SecurityHelper();

            var salt         = security.HashCreate();
            var passwordHash = security.HashCreate(entity.Password, salt);

            entity.Password = passwordHash.Split('æ')[0];
            entity.Salt     = passwordHash.Split('æ')[1];

            using (var scope = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.AcUsers.Add(entity);
                    _context.AcCategories.Add(new AcCategory {
                        CategoryName = "Project", User = entity
                    });
                    _context.SaveChanges();
                    scope.Commit();
                }
                catch
                {
                    scope.Rollback();
                    throw new Exception("An error occured while inserting user to table.");
                }
            }

            return(Task.FromResult(response));
        }
示例#5
0
        protected override Task <DeleteTodoResponse> ProcessCommand(DeleteTodoCommand command)
        {
            var response = new DeleteTodoResponse();

            var entity = _context.AcTasks.FirstOrDefault(e => e.TaskId == command.Data.TodoId && e.User.UserName == command.Data.UserName);

            entity.IsDeleted = true;

            _context.AcTasks.Attach(entity);

            var result = _context.SaveChanges();

            if (result == 0)
            {
                throw new System.Exception("An error occured while deleting task.");
            }
            return(Task.FromResult(response));
        }
示例#6
0
        public static void EnsureCreated(AppcentTodoContext context)
        {
            if (context.Database.EnsureCreated())
            {
                var user = new AcUser
                {
                    Email      = "*****@*****.**",
                    FirstName  = "Burak",
                    LastName   = "Portakal",
                    Password   = "******",//testtest
                    UserName   = "******",
                    Salt       = "tOoByYVHjUQ4Ue+SWZPmEQ==",
                    CreateDate = DateTime.Now
                };
                context.AcUsers.Add(user);


                context.AcTaskStatuses.Add(new AcTaskStatus {
                    Status = "Todo"
                });
                context.AcTaskStatuses.Add(new AcTaskStatus {
                    Status = "InProgress"
                });
                context.AcTaskStatuses.Add(new AcTaskStatus {
                    Status = "Completed"
                });

                context.AcTaskPriorities.Add(new AcTaskPriority {
                    Priority = "P1"
                });
                context.AcTaskPriorities.Add(new AcTaskPriority {
                    Priority = "P2"
                });
                context.AcTaskPriorities.Add(new AcTaskPriority {
                    Priority = "P3"
                });

                context.AcCategories.Add(new AcCategory {
                    CategoryName = "Project", User = user
                });

                context.AcTasks.Add(new AcTask
                {
                    Name           = "Test task",
                    CategoryId     = 1,
                    Status         = 1,
                    TaskPriorityId = 1,
                    User           = user,
                    IsDeleted      = false,
                    CreateDate     = DateTime.Now
                });

                context.AcTasks.Add(new AcTask
                {
                    Name           = "Test task 2",
                    CategoryId     = 1,
                    Status         = 1,
                    TaskPriorityId = 1,
                    User           = user,
                    IsDeleted      = false,
                    CreateDate     = DateTime.Now
                });
                context.SaveChanges();
            }
        }