Beispiel #1
0
        public Task <IdentityResult> CreateAsync(ChirpUser user, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            _db.Users.Add(user);

            try
            {
                _db.SaveChanges();
            }
            catch (Exception e)
            {
                if (e.IsPrimaryOrUniqueKeyViolation())
                {
                    return(Task.FromResult(IdentityResult.Failed(new IdentityError
                    {
                        Code = "err_username_taken", Description = $"Username {user.UsernameNormalized} is taken"
                    })
                                           ));
                }

                return(Task.FromResult(IdentityResult.Failed(new IdentityError
                {
                    Code = "err_internal_error", Description = e.Message
                })
                                       ));
            }

            return(Task.FromResult(IdentityResult.Success));
        }
Beispiel #2
0
        public override void Perform(long chirpId)
        {
            var chirp = _db.Chirps.Find(chirpId);

            if (chirp == null)
            {
                throw new Exception($"Chirp {chirpId} doesn't exist");
            }

            var hashTags = HashTagRegex.Matches(chirp.Contents).Select(m => m.Value).ToList();

            if (hashTags.Count == 0)
            {
                return;
            }

            if (_db.HashTags.Exists(hashTags[0].Substring(1), chirp.ChirpTimeUtc, chirp.Id))
            {
                return;                                                                              // idempotent
            }
            foreach (var tag in hashTags)
            {
                _db.HashTags.Add(new HashTag
                {
                    Tag     = tag.Substring(1),
                    TimeUtc = chirp.ChirpTimeUtc,
                    ChirpId = chirp.Id
                });
            }

            _db.SaveChanges();
        }
Beispiel #3
0
        public IActionResult Post(DtoUserRegister dto)
        {
            if (_db.Users.UserExists(dto.Username))
            {
                ModelState.AddModelError("Username", "This username is already taken");
            }

            if (dto.Password != dto.PasswordConfirm)
            {
                ModelState.AddModelError("PasswordConfirm", "The password and the confirmation password do not match");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = new ChirpUser
            {
                Username = dto.Username,
                Password = _passwordHasher.HashPassword(dto.Password)
            };

            _db.Users.Add(user);

            _db.SaveChanges();

            return(Ok(user.Id));
        }
Beispiel #4
0
        public IActionResult Post(DtoChirpPost dto)
        {
            _db.BeginTransaction();

            var chirp = new Chirp
            {
                UserId       = int.Parse(User.Identity.Name ?? "1"),
                ChirpTimeUtc = DateTime.UtcNow,
                ChirpType    = ChirpType.Chirp,
                Contents     = dto.Contents
            };

            _db.Chirps.Add(chirp);

            _db.SaveChanges();

            // Publish chirp processing job, before commit.
            // If the publishing fails, the transaction will be rolled back.
            //
            // We're using the job publish as the last committing resource because
            // it does not participate in the DB transaction.

            JobBatch.Do(() =>
            {
                TimelineUpdate.Publish(new TimelineUpdateArgs
                {
                    ChirpId  = chirp.Id,
                    AuthorId = chirp.UserId,
                    TimeUtc  = chirp.ChirpTimeUtc
                });

                HashTagUpdate.Publish(chirp.Id);
            });

            _db.CommitTransaction();

            return(Ok(chirp.Id));
        }