public Result <bool> AddProject(string email, ProjectProfilePage project)
        {
            var context       = new CrowDoDbContext();
            var projectExists = context.Set <ProjectProfilePage>().Where(p => p.Title == project.Title).Any();

            if (projectExists)
            {
                return(new Result <bool> {
                    ErrorCodeId = 1, ErrorCodeString = "Project already exists", Data = false
                });
            }
            var user = context.Set <User>().Where(c => c.Email == email).SingleOrDefault();

            if (user == null)
            {
                return(new Result <bool> {
                    ErrorCodeId = 2, ErrorCodeString = "Creator does not exist", Data = false
                });
            }
            var categoryExists = context.Set <Category>().Where(c => c.Name == project.Category).SingleOrDefault();;

            if (categoryExists == null)
            {
                var newCategory = new Category
                {
                    Name = project.Category
                };
                newCategory.Projects.Add(project);
                context.Add(newCategory);
            }
            else
            {
                categoryExists.Projects.Add(project);
                context.Update(categoryExists);
            }

            project.Creator = user;
            user.CreatedProjects.Add(project);
            context.Update(user);
            context.Add(project);
            if (context.SaveChanges() < 1)
            {
                return(new Result <bool> {
                    ErrorCodeId = 3, ErrorCodeString = "Not Saved", Data = false
                });
            }

            return(new Result <bool> {
                ErrorCodeId = 0, ErrorCodeString = "Project created", Data = true
            });
        }
Пример #2
0
        public void ReadCreatorFromFile(string fileName)
        { //Create new project
            using (StreamReader r = new StreamReader($@"{fileName}.json"))
            {
                string json = r.ReadToEnd();
                List <CreatorFromfile> creatorsFromFile = JsonConvert.DeserializeObject <List <CreatorFromfile> >(json);
                var users = new List <User>();

                foreach (CreatorFromfile c in creatorsFromFile)
                {
                    var user = new User
                    {
                        Name     = c.Name,
                        Email    = c.Email,
                        Location = c.Address,
                    };
                    users.Add(user);
                }
                var context = new CrowDoDbContext();
                foreach (User u in users)
                {
                    context.Add(u);
                    context.SaveChanges();
                }
            }
        }
Пример #3
0
        public Result <bool> CreateAccount(string name, string email, DateTime dateOfBirth, string location, string cardNumber)
        {
            var context = new CrowDoDbContext();

            if (IsValidEmail(email) == false)
            {
                return(new Result <bool> {
                    ErrorCodeId = 1, ErrorCodeString = "Invalid Email", Data = false
                });
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                return(new Result <bool> {
                    ErrorCodeId = 2, ErrorCodeString = "Null Or WhiteSpaceName", Data = false
                });
            }

            if (dateOfBirth.AddYears(18) > DateTime.Now)
            {
                return(new Result <bool> {
                    ErrorCodeId = 3, ErrorCodeString = "Not an adult", Data = false
                });
            }

            if (cardNumber.ToString().Length != 16)
            {
                return(new Result <bool> {
                    ErrorCodeId = 4, ErrorCodeString = "Invalid Card", Data = false
                });
            }

            var usrexist = context.Set <User>().Where(c => c.CardNumber == cardNumber).Any();

            if (usrexist)
            {
                return(new Result <bool> {
                    ErrorCodeId = 5, ErrorCodeString = "Card already exist, enter another card", Data = false
                });
            }


            var userexist = context.Set <User>().Where(u => u.Email == email).Any();


            if (userexist)
            {
                return(new Result <bool> {
                    ErrorCodeId = 6, ErrorCodeString = "Email belongs to another user", Data = false
                });
            }
            var user = new User
            {
                Name           = name,
                Email          = email,
                DateOfBirth    = dateOfBirth,
                DateOfRegister = DateTime.Now,
                Location       = location,
                CardNumber     = cardNumber
            };

            context.Add(user);


            var rowsAffected = context.SaveChanges();

            if (rowsAffected < 1)
            {
                return(new Result <bool> {
                    ErrorCodeId = 6, ErrorCodeString = "Nothing saved", Data = false
                });
            }

            return(new Result <bool> {
                ErrorCodeId = 0, ErrorCodeString = "User Created", Data = true
            });
        }