Exemplo n.º 1
0
        public ResponceModel <string> AddPicture(RequestModel <PhotoModel <Good> > request)
        {
            var responce = _userHelper.IsUserAutorized <string>(request.AuthKey);

            if (responce != null)
            {
                return(responce);
            }

            if (request.Content.Photo.Length == 0)
            {
                return(new ResponceModel <string>().FieldNotPass(nameof(request.Content.Photo)));
            }

            var good = _store.Goods.Where(f => f.ID == request.Content.Content.ID).FirstOrDefault();

            if (good == null)
            {
                return(new ResponceModel <string>().RecordNotFound());
            }

            string fileName = $"{Guid.NewGuid()}.{request.Content.PhotoType}";

            using (var fs = new FileStream($"img/{fileName}", FileMode.CreateNew))
                using (var ms = new MemoryStream(request.Content.Photo))
                    ms.CopyTo(fs);

            good.AddPicture(fileName);
            _store.SaveChanges();

            return(new ResponceModel <string> {
                content = fileName
            });
        }
Exemplo n.º 2
0
        public ResponceModel <Req> StoreOne <Req>(RequestModel <Req> request) where Req : IValidate
        {
            var responce = CheckRequest(request);

            if (responce != null)
            {
                return(responce);
            }

            _store.Add(request.Content);
            _store.SaveChanges();

            return(new ResponceModel <Req> {
                content = request.Content
            });
        }
Exemplo n.º 3
0
        public ResponceModel <UserAuthModel> Autorize(string loginOrEmail, string password)
        {
            if (string.IsNullOrEmpty(loginOrEmail) || string.IsNullOrEmpty(password))
            {
                return(new ResponceModel <UserAuthModel>().FieldEmptyError());
            }

            var foundUser = _store.Users.Where(f => f.Login == loginOrEmail).Include(f => f.UserSessions).FirstOrDefault();

            if (foundUser == null)
            {
                foundUser = _store.Users.Where(f => f.Email == loginOrEmail).Include(f => f.UserSessions).FirstOrDefault();
            }

            if (foundUser == null)
            {
                return(new ResponceModel <UserAuthModel>().UserNotFound());
            }

            if (!foundUser.IsPasswordRight(password))
            {
                return(new ResponceModel <UserAuthModel>().WrongPassword());
            }

            foundUser.UserSessions.Where(f => f.IsActive == true).ToList().ForEach(f => f.IsActive = false);

            UserSession session = new UserSession()
            {
                UserID = foundUser.ID
            };

            _store.Add(session);
            _store.SaveChanges();

            return(new ResponceModel <UserAuthModel>()
            {
                content = new UserAuthModel
                {
                    User = foundUser,
                    UserSession = session
                }
            });
        }
        public string DeleteToken(string nameLogin, string nametable, string oldToken)
        {
            try
            {
                switch (nametable)
                {
                case "admin":
                    // login by email
                    var adm = db.admins.Where(m => m.email.Equals(nameLogin)).FirstOrDefault();
                    if (db.tokens.Where(m => m.admin_id.Equals(adm.id) && m.value.Equals(oldToken)) != null)
                    {
                        db.Database.ExecuteSqlCommand("DELETE token WHERE admin_id = " + adm.id);
                        db.SaveChanges();
                    }
                    return("Delete token success!");

                case "users":
                    // login by username
                    var user = db.users.Where(m => m.username.Equals(nameLogin)).FirstOrDefault();
                    if (db.utokens.Where(m => m.user_id.Equals(user.id) && m.value.Equals(oldToken)) != null)
                    {
                        db.Database.ExecuteSqlCommand("DELETE utoken WHERE user_id = " + user.id);
                        db.SaveChanges();
                    }
                    return("Delete token success!");

                default:
                    break;
                }
                return("Delete token failed!");
            }
            catch (Exception)
            {
                return("Delete token failed!");
            }
        }
Exemplo n.º 5
0
        public string Edit_admin(string token, int admin_id, int id, string _username = null, string _pwd = null, string _email = null, string _address = null, string _phone = null, int?_level = null, string _image = null, string _fullname = null)
        {
            try
            {
                if (db.tokens.Where(m => m.value.Equals(token) && m.admin_id.Equals(admin_id)) != null)
                {
                    var record = db.admins.Where(m => m.id == id).FirstOrDefault();
                    record.username = _username ?? record.username;
                    record.pwd      = _pwd ?? record.pwd;
                    record.email    = _email ?? record.email;
                    record.address  = _address ?? record.address;
                    record.phone    = _phone ?? record.phone;
                    record.level    = _level ?? record.level;
                    record.image    = _image ?? record.image;
                    record.fullname = _fullname ?? record.fullname;

                    db.admins.AddOrUpdate(record);
                    db.SaveChanges();
                    return("Edit success!");
                }
            }
            catch (Exception)
            {
                return("Edit failed!");
            }
            return("Edit failed!");
        }
 public string Add_admin(string token, int admin_id, int id,
                         string _username = null, string _pwd      = null, string _email = null,
                         string _address  = null, string _phone    = null, int?_level    = null,
                         string _image    = null, string _fullname = null)
 {
     try
     {
         if (db.tokens.Where(m => m.value.Equals(token) &&
                             m.admin_id.Equals(admin_id)) != null)
         {
             db.admins.Add(new admin()
             {
                 id       = id,
                 username = _username,
                 pwd      = _pwd,
                 email    = _email,
                 address  = _address,
                 phone    = _phone,
                 level    = _level,
                 image    = _image,
                 fullname = _fullname
             });
             db.SaveChanges();
             return("Add success!");
         }
     }
     catch (Exception)
     {
         return("Add failed!");
     }
     return("Add failed!");
 }