Esempio n. 1
0
        private Result Register(User user)
        {
            if (user != null)
            {
                User existingUser = _db.Users.SingleOrDefault(u => u.Username == user.Username);
                if (existingUser != null)
                {
                    return(new Result(false)
                    {
                        Message = "username already in use"
                    });
                }
                else
                {
                    // no transactions so may fail
                    user.CreatedDate = DateTime.UtcNow;
                    _db.Users.Add(user);
                    _db.SaveChanges();

                    return(new Result(true));
                }
            }

            return(new Result(false)
            {
                Message = "invalid input"
            });
        }
Esempio n. 2
0
        public LessonComment AddComment(LessonComment comment)
        {
            LessonComment insertComment = _ctx.LessonComments.Add(comment);

            _ctx.SaveChanges();

            return(insertComment);
        }
Esempio n. 3
0
        public void ImportSuburbs(MyHobbyContext context)
        {
            List <Suburb> suburbs = new List <Suburb>();

            StreamReader sr = null;

            try
            {
                City hk = context.Cities.SingleOrDefault(c => c.EnglishName == "Hong Kong");
                if (hk != null)
                {
                    return;
                }

                hk = new City()
                {
                    Name = "香港", EnglishName = "Hong Kong"
                };
                context.Cities.Add(hk);

                Assembly resourceAssembly = Assembly.GetAssembly(typeof(MyHobby.Resources.LocateThisAssembly));
                Stream   stream           = resourceAssembly.GetManifestResourceStream("MyHobby.Resources.Suburbs_HK.txt");

                sr = new StreamReader(stream);
                if (sr != null)
                {
                    string      line  = sr.ReadLine();
                    SuburbGroup group = null;

                    while (!string.IsNullOrEmpty(line))
                    {
                        string[] fields = SplitCSV(line); //line.Split(',');

                        if (fields.Length == 3)
                        {
                            group = new SuburbGroup()
                            {
                                Name        = fields[1],
                                EnglishName = fields[2],
                                City        = hk
                            };

                            context.SuburbGroups.Add(group);
                        }
                        else
                        {
                            Suburb suburb = new Suburb()
                            {
                                Name        = fields[0],
                                EnglishName = fields[1],
                                SuburbGroup = group
                            };

                            context.Suburbs.Add(suburb);
                        }

                        line = sr.ReadLine();
                    }

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }
        }
Esempio n. 4
0
 public void AddReview(BusinessReview review)
 {
     _ctx.BusinessReviews.Add(review);
     _ctx.SaveChanges();
 }