Ejemplo n.º 1
0
 public void UpdateAlbum(ref Album album, int accountId)
 {
     album.Modified   = DateTime.UtcNow;
     album.ModifiedBy = accountId;
     RepositoryContext.Albums.Update(album);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 2
0
 public void CreateAlbum(ref Album album, int accountId)
 {
     album.Created   = DateTime.UtcNow;
     album.CreatedBy = accountId;
     RepositoryContext.Albums.Add(album);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 3
0
 public void UpdateEvent(ref Event calendarEvent, int accountId)
 {
     calendarEvent.Updated   = DateTime.UtcNow;
     calendarEvent.UpdatedBy = accountId;
     RepositoryContext.Events.Update(calendarEvent);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 4
0
 public void DeactivateOfficer(Officer officer, int accountId)
 {
     officer.Active    = false;
     officer.Updated   = DateTime.UtcNow;
     officer.UpdatedBy = accountId;
     RepositoryContext.Officers.Update(officer);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 5
0
        public void SaveVerificationToken(string token, ref Account account)
        {
            account.Verified          = DateTime.UtcNow;
            account.VerificationToken = null;

            RepositoryContext.Accounts.Update(account);
            RepositoryContext.SaveChanges();
        }
Ejemplo n.º 6
0
 public Content UpdateContent(ref Content content, int accountId)
 {
     content.Updated           = DateTime.UtcNow;
     content.LastUpdatedUserId = accountId;
     RepositoryContext.Update(content);
     RepositoryContext.SaveChanges();
     return(content);
 }
Ejemplo n.º 7
0
 public void DeactivateCommitteeMember(ref CommitteeMember member, int accountId)
 {
     member.Active    = false;
     member.Updated   = DateTime.UtcNow;
     member.UpdatedBy = accountId;
     RepositoryContext.CommitteeMembers.Update(member);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 8
0
        public Account Update(ref Account account)
        {
            account.Updated = DateTime.UtcNow;
            RepositoryContext.Accounts.Update(account);
            RepositoryContext.SaveChanges();

            return(account);
        }
Ejemplo n.º 9
0
        public Account Create(ref Account account)
        {
            // save account

            account.Created  = DateTime.UtcNow;
            account.Verified = DateTime.UtcNow;
            RepositoryContext.Accounts.Add(account);
            RepositoryContext.SaveChanges();
            return(account);
        }
Ejemplo n.º 10
0
        public void RevokeToken(string token, string ipAddress)
        {
            var(refreshToken, account) = getRefreshToken(token);

            // revoke token and save
            refreshToken.Revoked     = DateTime.UtcNow;
            refreshToken.RevokedByIp = ipAddress;
            RepositoryContext.Update(account);
            RepositoryContext.SaveChanges();
        }
Ejemplo n.º 11
0
        public void Authenticate(string ipAddress, string secret, ref Account account, out string jwtToken, out RefreshToken refreshToken)
        {
            // authentication successful so generate jwt and refresh tokens
            jwtToken     = generateJwtToken(account, secret);
            refreshToken = generateRefreshToken(ipAddress);

            // save refresh token
            account.RefreshTokens?.Add(refreshToken);
            RepositoryContext.Update(account);
            RepositoryContext.SaveChanges();
        }
Ejemplo n.º 12
0
        public void ResetPassword(ResetPasswordRequest model, Account account)
        {
            // update password and remove reset token
            account.PasswordHash      = BC.HashPassword(model.Password);
            account.PasswordReset     = DateTime.UtcNow;
            account.ResetToken        = null;
            account.ResetTokenExpires = null;

            RepositoryContext.Accounts.Update(account);
            RepositoryContext.SaveChanges();
        }
Ejemplo n.º 13
0
        public void CreatePhotos(ref IEnumerable <Photo> photos, int accountId)
        {
            foreach (var photo in photos)
            {
                photo.Created = DateTime.UtcNow;
                photo.AddedBy = accountId;
                RepositoryContext.Photos.Add(photo);
            }

            RepositoryContext.SaveChanges();
        }
Ejemplo n.º 14
0
        public Officer UpdateOfficer(ref Officer officer, int accountId, int?memberId)
        {
            if (memberId != null)
            {
                officer.MemberId = memberId;
            }

            officer.Updated   = DateTime.UtcNow;
            officer.UpdatedBy = accountId;
            RepositoryContext.Officers.Update(officer);
            RepositoryContext.SaveChanges();
            return(officer);
        }
Ejemplo n.º 15
0
        public CommitteeMember UpdateCommitteeMember(ref CommitteeMember member, int accountId, int?memberId)
        {
            if (memberId != null)
            {
                member.MemberId = memberId;
            }

            member.Updated   = DateTime.UtcNow;
            member.UpdatedBy = accountId;
            RepositoryContext.CommitteeMembers.Update(member);
            RepositoryContext.SaveChanges();
            return(member);
        }
Ejemplo n.º 16
0
        public void Register(RegisterRequest model, string origin, Account account, IEmailService emailService)
        {
            // first registered account is an admin
            var isFirstAccount = !RepositoryContext.Accounts.Any();

            account.Role              = isFirstAccount ? Role.Admin : Role.Member;
            account.Created           = DateTime.UtcNow;
            account.VerificationToken = randomTokenString();

            // save account
            RepositoryContext.Accounts.Add(account);
            RepositoryContext.SaveChanges();

            // send email
            sendVerificationEmail(account, origin, emailService);
        }
Ejemplo n.º 17
0
        public Officer AddOfficer(ref Officer officer, int?accountId, int?memberId)
        {
            if (accountId != null)
            {
                officer.CreatedBy = accountId;
            }

            if (memberId != null)
            {
                officer.MemberId = memberId;
            }

            officer.Created = DateTime.UtcNow;
            RepositoryContext.Officers.Add(officer);
            RepositoryContext.SaveChanges();
            return(officer);
        }
Ejemplo n.º 18
0
        public Account RefreshToken(string token, string ipAddress, string secret, out string jwtToken, out RefreshToken newRefreshToken)
        {
            var(refreshToken, account) = getRefreshToken(token);

            // replace old refresh token with a new one and save
            newRefreshToken              = generateRefreshToken(ipAddress);
            refreshToken.Revoked         = DateTime.UtcNow;
            refreshToken.RevokedByIp     = ipAddress;
            refreshToken.ReplacedByToken = newRefreshToken.Token;
            account.RefreshTokens.Add(newRefreshToken);
            RepositoryContext.Update(account);
            RepositoryContext.SaveChanges();

            // generate new jwt
            jwtToken = generateJwtToken(account, secret);
            return(account);
        }
        public static void Initialize(RepositoryContext context)
        {
            context.Database.Migrate();

            if (context.Users.Any())
            {
                return;
            }

            context.Users.Add(new User()
            {
                // Todo: вынести логин и пароль в кофиг
                Name         = "admin",
                PasswordHash = IdentityHelper.GetPasswordHash("admin"),
                Role         = Role.Admin
            });

            context.SaveChanges();
        }
Ejemplo n.º 20
0
        public void ForgotPassword(ForgotPasswordRequest model, string origin, IEmailService emailService)
        {
            var account = RepositoryContext.Accounts.SingleOrDefault(x => x.Email == model.Email);

            // always return ok response to prevent email enumeration
            if (account == null)
            {
                return;
            }

            // create reset token that expires after 1 day
            account.ResetToken        = randomTokenString();
            account.ResetTokenExpires = DateTime.UtcNow.AddDays(24);

            RepositoryContext.Accounts.Update(account);
            RepositoryContext.SaveChanges();

            // send email
            sendPasswordResetEmail(account, origin, emailService);
        }
Ejemplo n.º 21
0
        public CommitteeMember CreateCommitteeMember(ref CommitteeMember member, int?accountId, int?memberId)
        {
            member.Created = DateTime.UtcNow;
            if (accountId == null)
            {
            }
            else
            {
                member.CreatedBy = accountId;
            }

            if (memberId != null)
            {
                member.MemberId = memberId;
            }

            RepositoryContext.CommitteeMembers.Add(member);
            RepositoryContext.SaveChanges();
            return(member);
        }
Ejemplo n.º 22
0
 public void Save()
 {
     _repositoryContext.SaveChanges();
 }
Ejemplo n.º 23
0
 public void Save()
 {
     _repoContext.SaveChanges();
 }
Ejemplo n.º 24
0
 public void DeletePhoto(Photo photo)
 {
     RepositoryContext.Photos.Remove(photo);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 25
0
        //Kiểm tra collection truyền vào có tên trùng trong database không
        //KQ: false = TruyenID hoặc TheLoaiID không tồn tại, true: thêm thành công
        public ResponseDetails CreateNoiDungChuong(IEnumerable <NoiDungChuong> noiDungChuongs)
        {
            /*Kiểm tra xem chuỗi json nhập vào có bị trùng tên chương không*/
            foreach (var dup in noiDungChuongs.GroupBy(p => p.HinhAnh))
            {
                if (dup.Count() - 1 > 0)
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Chuỗi json nhập vào bị trùng đường dẫn ảnh",
                        Value = dup.Key.ToString()
                    });
                }
            }
            /*End*/

            var chuongRepo = new ChuongRepository(_context);

            foreach (var nd in noiDungChuongs)
            {
                /*Bắt lỗi [ID]*/
                if (!chuongRepo.FindByCondition(t => t.ChuongID.Equals(nd.ChuongID)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "ID chương không tồn tại",
                        Value = nd.ChuongID.ToString()
                    });
                }
                /*End*/

                /*Bắt lỗi [Tên hình ảnh]*/
                if (FindByCondition(t => t.HinhAnh.Equals(nd.HinhAnh) && t.ChuongID.Equals(nd.ChuongID)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Chương đã tồn tại hình ảnh này",
                        Value = nd.HinhAnh
                    });
                }

                if (FindByCondition(t => t.HinhAnh.Equals(nd.HinhAnh)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Hình ảnh này đã nằm trong 1 chương khác",
                        Value = nd.HinhAnh
                    });
                }
                /*End*/

                //Tạo dữ liệu nhưng chưa add vào CSDL
                Create(nd);
                _context.SaveChanges();
            }

            return(new ResponseDetails()
            {
                StatusCode = ResponseCode.Success, Message = "Thêm nội dung chương thành công"
            });
        }
Ejemplo n.º 26
0
        //Kiểm tra collection truyền vào có tên trùng trong database không
        //KQ: !null = TenChuong bị trùng, null: thêm thành công
        public ResponseDetails CreateChuong(IEnumerable <Chuong> chuongs)
        {
            /*Kiểm tra xem chuỗi json nhập vào có bị trùng tên chương không*/
            foreach (var dup in chuongs.GroupBy(p => p.TenChuong))
            {
                if (dup.Count() - 1 > 0)
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Chuỗi json nhập vào bị trùng tên chương",
                        Value = dup.Key.ToString()
                    });
                }
            }
            /*End*/

            foreach (var chuong in chuongs)
            {
                /*Bắt lỗi [ID]*/
                var truyenRepo = new TruyenRepository(_context);
                if (!truyenRepo.FindByCondition(t => t.TruyenID.Equals(chuong.TruyenID)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "ID truyện không tồn tại",
                        Value = chuong.TruyenID.ToString()
                    });
                }
                /*End*/

                /*Bắt lỗi [Tên chương]*/
                if (FindByCondition(t => t.TenChuong.Equals(chuong.TenChuong) && t.TruyenID.Equals(chuong.TruyenID)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Tên chương bị trùng",
                        Value = chuong.TenChuong
                    });
                }
                /*End*/

                var found = FindByCondition(m => m.TruyenID.Equals(chuong.TruyenID));
                if (found.Count() > 0)
                {
                    chuong.STT = found.Max(m => m.STT) + 1;
                }
                else
                {
                    chuong.STT = 1;
                }
                chuong.ThoiGianCapNhat = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                Random r = new Random();
                chuong.LuotXem = r.Next(5000, 15000);

                //Tạo dữ liệu nhưng chưa add vào CSDL
                Create(chuong);
                _context.SaveChanges();
            }
            return(new ResponseDetails()
            {
                StatusCode = ResponseCode.Success
            });
        }
Ejemplo n.º 27
0
 public void Delete(Account account)
 {
     RepositoryContext.Accounts.Remove(account);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 28
0
 public void DeleteAlbum(Album album)
 {
     RepositoryContext.Albums.Remove(album);
     RepositoryContext.SaveChanges();
 }
Ejemplo n.º 29
0
        //Kiểm tra collection truyền vào có tên trùng trong database không
        //KQ: !null = TenTruyen bị trùng, null: thêm thành công
        public ResponseDetails CreateTruyen(IEnumerable <Truyen> truyens)
        {
            /*Kiểm tra xem chuỗi json nhập vào có bị trùng tên truyện không*/
            foreach (var dup in truyens.GroupBy(p => p.TenTruyen))
            {
                if (dup.Count() - 1 > 0)
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Chuỗi json nhập vào bị trùng tên truyện",
                        Value = dup.Key.ToString()
                    });
                }
            }
            /*End*/

            /*Kiểm tra xem chuỗi json nhập vào có bị trùng hình ảnh không*/
            foreach (var dup in truyens.GroupBy(p => p.HinhAnh))
            {
                if (dup.Count() - 1 > 0)
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Chuỗi json nhập vào bị trùng hình ảnh truyện",
                        Value = dup.Key.ToString()
                    });
                }
            }
            /*End*/

            var tacGiaRepo = new TacGiaRepository(_context);

            foreach (var truyen in truyens)
            {
                /*Bắt lỗi [ID]*/
                if (!tacGiaRepo.FindByCondition(t => t.TacGiaID.Equals(truyen.TacGiaID)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "ID tác giả không tồn tại",
                        Value = truyen.TacGiaID.ToString()
                    });
                }
                /*End*/

                /*Bắt lỗi [Tên truyện]*/
                if (FindByCondition(t => t.TenTruyen.Equals(truyen.TenTruyen)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Tên truyện bị trùng",
                        Value = truyen.TenTruyen
                    });
                }
                /*End*/

                /*Bắt lỗi [Hình ảnh]*/
                if (FindByCondition(t => t.HinhAnh.Equals(truyen.HinhAnh)).Any())
                {
                    return(new ResponseDetails()
                    {
                        StatusCode = ResponseCode.Error,
                        Message = "Hình ảnh này đã tồn tại",
                        Value = truyen.TenTruyen + ": " + truyen.HinhAnh
                    });
                }
                /*End*/

                truyen.TenTruyen = truyen.TenTruyen.ToLower();
                Create(truyen);
                _context.SaveChanges();
            }

            return(new ResponseDetails()
            {
                StatusCode = ResponseCode.Success
            });
        }
Ejemplo n.º 30
0
 protected void TrySave()
 {
     RepositoryContext.SaveChanges();
 }