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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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); } }