コード例 #1
0
ファイル: OperationsReadWrite.cs プロジェクト: timmyjoel/TRec
        public static List <ValidationMessage> SaveProject(
            ValidatableParameter <string> projectId,
            ValidatableParameter <string> details,
            ValidatableParameter <bool> active
            )
        {
            List <ValidationMessage> errors = new List <ValidationMessage>();

            Guid projId = Guid.Empty;

            try
            {
                projId = new Guid(projectId.Value);
            }
            catch (FormatException)
            {
                errors.Add(new ValidationMessage {
                    MessageText = "Project Id must be in the format dddddddd-dddd-dddd-dddd-dddddddddddd", Source = projectId.Source
                });
            }

            if (string.IsNullOrEmpty(details.Value) || details.Value.Trim().Length == 0)
            {
                errors.Add(new ValidationMessage {
                    MessageText = "Project Name must be supplied", Source = details.Source
                });
            }
            else
            {
                //check for existing name with different id (name already in use)
                Project existItem = AllProjects.GetForName(details.Value);
                if (existItem != null && existItem.Id != projId)
                {
                    errors.Add(new ValidationMessage {
                        MessageText = string.Format("Project Name is already in use by project with Id {0}", existItem.Id.ToString()), Source = details.Source
                    });
                }
            }

            //perform update if no errors
            if (errors.Count == 0)
            {
                AllProjects items   = new AllProjects();
                Project     newItem = new Project {
                    Id = projId, Description = details.Value, Active = active.Value
                };
                if (projId == Guid.Empty)
                {
                    newItem.Id = Guid.NewGuid(); //set id
                    items.Add(newItem);
                }
                else
                {
                    Project existItem = items.Where(i => i.Id == projId).Single();
                    items.Remove(existItem);
                    items.Add(newItem);
                }

                items.Save();
            }

            return(errors);
        }