Пример #1
0
 public Models.Action GetAction(int id)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Models.Action>().Find(id));
     }
 }
Пример #2
0
 public User GetUser(int id)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <User>().Find(id));
     }
 }
Пример #3
0
 public Field GetField(int id)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Field>().Find(id));
     }
 }
Пример #4
0
 public Project GetProject(int id)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Project>().Find(id));
     }
 }
Пример #5
0
 public ICollection <Project> GetProjects()
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Project>().OrderBy(x => x.Id).ToList());
     }
 }
Пример #6
0
        public Models.Action UpdateAction(int actionId, string parties, string description, string comment, ActionPriority priority, ActionStatus status = ActionStatus.Open)
        {
            RequireMemberAccess();
            var action = GetAction(actionId);

            if (action == null)
            {
                throw new ClientException("the specified action is not exist");
            }
            else if (action.Uid != _currentUser.Id && !_currentUser.IsAdmin)
            {
                throw new ClientException("you cannot edit other's action");
            }

            if (string.IsNullOrWhiteSpace(description))
            {
                throw new ClientException("description must not be blank");
            }

            using (var _dbContext = new DailyReportsContext())
            {
                action             = _dbContext.Actions.Where(x => x.Id == actionId).FirstOrDefault();
                action.Parties     = parties;
                action.Description = description;
                action.Comment     = comment;
                action.Priority    = priority;
                action.Status      = status;
                action.Update      = DateTime.UtcNow;
                _dbContext.SaveChanges();

                return(action);
            }
        }
Пример #7
0
 public Record GetRecord(int id)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Record>().Find(id));
     }
 }
Пример #8
0
 public Models.Action GetMyAction(int id)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Models.Action>().Where(x => x.Uid == _currentUser.Id && x.Id == id).OrderBy(x => x.Id).FirstOrDefault());
     }
 }
Пример #9
0
 public User GetUser(string domainName)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Users.Where(x => x.DomainName == domainName).FirstOrDefault());
     }
 }
Пример #10
0
 public ICollection <Models.Action> GetMyActions()
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Models.Action>().Where(x => x.Uid == _currentUser.Id).OrderBy(x => x.Id).ToList());
     }
 }
Пример #11
0
 public ICollection <Models.Action> GetOpenActions()
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Models.Action>().Where(x => x.Status == ActionStatus.Open).OrderBy(x => x.Id).ToList());
     }
 }
Пример #12
0
 public Record GetMyRecord(int id)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Record>().Where(x => x.Uid == _currentUser.Id && x.Id == id).FirstOrDefault());
     }
 }
Пример #13
0
        public Record UpdateRecord(int recordId, int fieldId, string destination, bool turnover, string detail)
        {
            RequireMemberAccess();
            var record = GetRecord(recordId);

            if (record == null)
            {
                throw new ClientException("the specified record is not exist");
            }
            else if (record.Uid != _currentUser.Id && !_currentUser.IsAdmin)
            {
                throw new ClientException("you cannot edit other's record");
            }

            var field = GetField(fieldId);

            if (field == null)
            {
                throw new ClientException("the specified field is not exist");
            }
            else if (field.Parent == 0)
            {
                throw new ClientException("cannot append a record to top field");
            }

            if (string.IsNullOrWhiteSpace(destination))
            {
                throw new ClientException("destination must not be blank");
            }

            using (var _dbContext = new DailyReportsContext())
            {
                // relationship validation
                var nowDate = DateTime.UtcNow.Date;
                if (_dbContext.Records.Any(x => DbFunctions.TruncateTime(x.Create) == nowDate && x.FieldId == fieldId && x.Id != recordId))
                {
                    throw new ClientException("the specified field has been fill today");
                }

                record             = _dbContext.Records.Where(x => x.Id == recordId).FirstOrDefault();
                record.FieldId     = fieldId;
                record.Update      = DateTime.UtcNow;
                record.UpdateBy    = _currentUser.Id;
                record.Destination = destination;
                record.TurnOver    = turnover;
                record.Detail      = detail;
                _dbContext.SaveChanges();

                // update the field's TurnOver if turnover from UI is true
                if (turnover)
                {
                    this.UpdateField(fieldId, field.Name, field.Destination, field.Start, field.End, field.ProjectId, field.Parent, nowDate);
                }

                return(record);
            }
        }
Пример #14
0
        public Field UpdateField(int fieldId, string name, string destination, DateTime start, DateTime end, int projectId, int parent = 0, DateTime?turnover = null)
        {
            RequireAdminAccess();
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ClientException("field name must be provided");
            }
            if (parent != 0 && string.IsNullOrWhiteSpace(destination))
            {
                throw new ClientException("sub field must have a destination for sample usage");
            }

            var project = GetProject(projectId);

            if (project == null)
            {
                throw new ClientException("project you specified is not exist");
            }

            Field parentField = null;

            if (parent != 0)
            {
                parentField = GetField(parent);
                if (parentField == null)
                {
                    throw new ClientException("the parent field you specified is not exist");
                }

                projectId = parentField.ProjectId; // sub field must have same project id with parent
            }


            using (var _dbContext = new DailyReportsContext())
            {
                var field = _dbContext.Fields.Where(x => x.Id == fieldId).FirstOrDefault();
                if (field == null)
                {
                    throw new ClientException("The specified field is not existed");
                }
                field.Name        = name;
                field.Destination = destination;
                field.Start       = start.Date;
                field.End         = end.Date;
                field.Parent      = parent;
                field.ProjectId   = projectId;
                if (turnover != null)
                {
                    field.TurnOver = ((DateTime)turnover).Date;
                }

                _dbContext.SaveChanges();

                return(field);
            }
        }
Пример #15
0
        public Field AddField(string name, string destination, DateTime start, DateTime end, int projectId, int parent = 0)
        {
            RequireAdminAccess();
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ClientException("field name must be provided");
            }
            if (parent != 0 && string.IsNullOrWhiteSpace(destination))
            {
                throw new ClientException("sub field must have a destination for sample usage");
            }

            var project = GetProject(projectId);

            if (project == null)
            {
                throw new ClientException("project you specified is not exist");
            }

            Field parentField = null;

            if (parent != 0)
            {
                parentField = GetField(parent);
                if (parentField == null)
                {
                    throw new ClientException("the parent field you specified is not exist");
                }

                projectId = parentField.ProjectId; // sub field must have same project id with parent
            }


            using (var _dbContext = new DailyReportsContext())
            {
                var exist = _dbContext.Fields.Any(x => x.Name == name && x.ProjectId == projectId);
                if (exist)
                {
                    throw new ClientException("The field is existed");
                }
                var field = _dbContext.Fields.Add(new Field()
                {
                    Name        = name,
                    Destination = destination,
                    Start       = start.Date,
                    End         = end.Date,
                    Parent      = parent,
                    ProjectId   = projectId
                });

                _dbContext.SaveChanges();

                return(field);
            }
        }
Пример #16
0
 public ICollection <Field> GetFields(DateTime?date)
 {
     if (date == null)
     {
         date = DateTime.UtcNow.Date;
     }
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Field>().Where(x => x.Start <= date && x.End >= date).OrderBy(x => x.Id).ToList());
     }
 }
Пример #17
0
 public ICollection <Record> GetMyRecords(DateTime?date)
 {
     if (date == null)
     {
         date = DateTime.UtcNow.Date;
     }
     using (var _dbContext = new DailyReportsContext())
     {
         return(_dbContext.Set <Record>().Where(x => DbFunctions.TruncateTime(x.Create) == date && x.Uid == _currentUser.Id).OrderBy(x => x.Id).ToList());
     }
 }
Пример #18
0
        public Record AddRecord(int fieldId, string destination, bool turnover, string detail)
        {
            RequireMemberAccess();
            var user = GetUser(_currentUser.Id);

            if (user == null)
            {
                throw new ClientException("the specified user is not a member");
            }
            var field = GetField(fieldId);

            if (field == null)
            {
                throw new ClientException("the specified field is not exist");
            }
            if (field.Parent == 0)
            {
                throw new ClientException("cannot append a record to top field");
            }
            if (string.IsNullOrWhiteSpace(destination))
            {
                throw new ClientException("destination must not be blank");
            }

            using (var _dbContext = new DailyReportsContext())
            {
                var nowDate = DateTime.UtcNow.Date;
                // relationship validation
                // http://stackoverflow.com/questions/14601676/the-specified-type-member-date-is-not-supported-in-linq-to-entities-only-init
                if (_dbContext.Records.Any(x => DbFunctions.TruncateTime(x.Create) == nowDate && x.FieldId == fieldId))
                {
                    throw new ClientException("the specified field has been filled today");
                }

                var record = _dbContext.Records.Add(new Record()
                {
                    Uid         = _currentUser.Id,
                    FieldId     = fieldId,
                    Destination = destination,
                    Create      = DateTime.UtcNow,
                    Detail      = detail
                });
                _dbContext.SaveChanges();

                // update the field's TurnOver if turnover from UI is true
                if (turnover)
                {
                    this.UpdateField(fieldId, field.Name, field.Destination, field.Start, field.End, field.ProjectId, field.Parent, nowDate);
                }

                return(record);
            }
        }
Пример #19
0
        public Project AddProject(string name, string description)
        {
            RequireOwnerAccess();
            using (var _dbContext = new DailyReportsContext())
            {
                var exist = _dbContext.Projects.Any(x => x.Name == name);
                if (exist)
                {
                    throw new ClientException("The project is existed");
                }
                var project = _dbContext.Projects.Add(new Project()
                {
                    Name        = name,
                    Description = description
                });

                _dbContext.SaveChanges();

                return(project);
            }
        }
Пример #20
0
 public ModelsManager(DomainName domainName)
 {
     using (var _dbContext = new DailyReportsContext())
     {
         var fullName  = domainName.GetFullName();
         var existUser = _dbContext.Users.Where(x => x.DomainName == fullName).FirstOrDefault();
         if (existUser != null)
         {
             _currentUser = existUser;
         }
         else
         {
             _currentUser = new User()
             {
                 Name       = domainName.GetName(),
                 DomainName = domainName.GetFullName(),
                 Role       = UserRole.Guest,
                 Id         = 0
             };
         }
     }
 }
Пример #21
0
        public Models.Action AddAction(int projectId, string parties, string description, string comment, ActionPriority priority, ActionStatus status = ActionStatus.Open)
        {
            RequireMemberAccess();
            var user = GetUser(_currentUser.Id);

            if (user == null)
            {
                throw new ClientException("the specified user is not a member");
            }
            var project = GetProject(projectId);

            if (project == null)
            {
                throw new ClientException("the specified project is not exist");
            }
            if (string.IsNullOrWhiteSpace(description))
            {
                throw new ClientException("description must not be blank");
            }

            using (var _dbContext = new DailyReportsContext())
            {
                var action = _dbContext.Actions.Add(new Models.Action()
                {
                    Uid         = _currentUser.Id,
                    ProjectId   = projectId,
                    Parties     = parties,
                    Description = description,
                    Create      = DateTime.UtcNow,
                    Comment     = comment,
                    Priority    = priority,
                    Status      = status
                });
                _dbContext.SaveChanges();

                return(action);
            }
        }
Пример #22
0
        public User AddUser(string name, string domainName, UserRole role = UserRole.User)
        {
            RequireAdminAccess();
            using (var _dbContext = new DailyReportsContext())
            {
                var exist = _dbContext.Users.Any(x => x.Name == name || x.DomainName == domainName);
                if (exist)
                {
                    throw new ClientException("The user is existed");
                }
                var user = _dbContext.Users.Add(new User()
                {
                    Name       = name,
                    DomainName = domainName,
                    Role       = role,
                    Create     = DateTime.UtcNow
                });

                _dbContext.SaveChanges();

                return(user);
            }
        }
Пример #23
0
        public User AddOwner(string name, string domainName)
        {
            var users = GetUsers();

            if (users.Count() > 0)
            {
                throw new ClientException("Owner must be the first user");
            }

            using (var _dbContext = new DailyReportsContext())
            {
                var user = _dbContext.Users.Add(new User()
                {
                    Name       = name,
                    DomainName = domainName,
                    Role       = UserRole.Owner,
                    Create     = DateTime.UtcNow
                });

                _dbContext.SaveChanges();

                return(user);
            }
        }
Пример #24
0
        public static void PrepareDb()
        {
            SqlConnection connection = Aspen.DailyUpdates.DBModel.Database.Connection;

            if (System.Data.Entity.Database.Exists(connection))
            {
                Console.WriteLine("Database exist, enter to drop\r\n");
                Console.ReadKey();

                DailyReportsContext context = new DailyReportsContext();
                context.Database.Delete();
                Console.WriteLine("Database was deleted\r\n");

                Console.WriteLine("Enter to create new database\r\n");
                Console.ReadKey();

                Console.WriteLine("Adding new database\r\n");
                context.Database.Create();
                Console.WriteLine("Database created\r\n");

                Console.WriteLine("Enter to continue\r\n");
                Console.ReadKey();
            }
        }