Esempio n. 1
0
        public async Task <byte[]> GetImage(HttpContext context)
        {
            try
            {
                var path1 = Path.Combine("Resources", "Images");
                var path  = Path.Combine(path1, "Avatars");

                string userName  = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);
                string imageName = userName;

                //var fileName = Directory.EnumerateFiles(@path, imageName).FirstOrDefault();

                var fileName = Path.Combine(path, imageName);

                using var memory = new MemoryStream();
                using (var stream = new FileStream(fileName, FileMode.Open))
                {
                    await stream.CopyToAsync(memory);
                }
                memory.Position = 0;

                return(memory.GetBuffer());
            }
            catch (Exception)
            {
                throw new ErrorException(ErrorCode.ImageNotFound, "Slika nije pronađena.");
            }
        }
Esempio n. 2
0
        public async Task <List <OutProizvodDTO> > GetAllForUser(HttpContext context)
        {
            string userId = TokensHelper.GetClaimFromJwt(context, CustomClaims.UserId.ToString());

            var proizvodi = await _db.Proizvodi.Include(i => i.Prodavac).Where(p => p.Prodavac.Id == userId).ToListAsync();

            List <OutProizvodDTO> outProizvodi = new List <OutProizvodDTO>();

            foreach (var proizvod in proizvodi)
            {
                string slika = null;

                try
                {
                    using var buffer = await GetImage(proizvod.Id);

                    slika = Convert.ToBase64String(buffer.GetBuffer());
                }
                catch (Exception) { }

                outProizvodi.Add(new OutProizvodDTO
                {
                    Id              = proizvod.Id,
                    Naziv           = proizvod.Naziv,
                    Cena            = proizvod.Cena,
                    Opis            = null,
                    NacinKoriscenja = proizvod.NacinKoriscenja,
                    Prodavac        = null,
                    Slika           = slika
                });
            }

            return(outProizvodi);
        }
Esempio n. 3
0
        public async Task <bool> Delete(HttpContext context)
        {
            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                return(true);
            }

            var res = await _userManager.DeleteAsync(user);

            try
            {
                DeleteImage(context);
            }
            catch (Exception) { }

            if (!res.Succeeded)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        public async Task <Account> Update(HttpContext context, Update model)
        {
            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Korisnik ne postoji u sistemu.");
            }

            user.FirstName   = model.FirstName;
            user.LastName    = model.LastName;
            user.PhoneNumber = model.PhoneNumber;
            user.Address     = model.Address;

            var res = await _userManager.UpdateAsync(user);

            if (!res.Succeeded)
            {
                throw new ErrorException(ErrorCode.UserUpdateError, "Greška pri čuvanju profila.");
            }

            return(new Account
            {
                Username = user.UserName,
                Email = user.Email,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Address = user.Address,
                PhoneNumber = user.PhoneNumber
            });
        }
Esempio n. 5
0
        public async Task <OutProizvodDTO> Add(InProductDTO model, HttpContext context)
        {
            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Prodavac ne postoji u sistemu.");
            }

            Guid id = Guid.NewGuid();

            _db.Proizvodi.Add(new Proizvod
            {
                Id              = id,
                Naziv           = model.Naziv,
                Cena            = model.Cena,
                Opis            = model.Opis,
                NacinKoriscenja = model.NacinKoriscenja,
                Prodavac        = user
            });

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw new ErrorException(ErrorCode.DbError, "Greška pri čuvanju proizvoda u bazu podataka.");
            }

            return(new OutProizvodDTO
            {
                Id = id,
                Naziv = model.Naziv,
                Cena = model.Cena,
                Opis = model.Opis,
                NacinKoriscenja = model.NacinKoriscenja,
                Prodavac = null
            });
        }
Esempio n. 6
0
        public bool DeleteImage(HttpContext context)
        {
            try
            {
                var path1 = Path.Combine("Resources", "Images");
                var path  = Path.Combine(path1, "Avatars");

                string userName  = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);
                string imageName = userName;

                var fullPath = Path.Combine(path, imageName);

                File.Delete(fullPath);

                return(true);
            }
            catch (Exception)
            {
                throw new ErrorException(ErrorCode.ImageNotFound, "Greška pri brisanju slike.");
            }
        }
Esempio n. 7
0
        public async Task <UserAuthData> ChangePassword(HttpContext context, ChangePassword change)
        {
            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Korisnik ne postoji u sistemu.");
            }

            var res = await _userManager.ChangePasswordAsync(user, change.OldPassword, change.NewPassword);

            if (!res.Succeeded)
            {
                throw new ErrorException(ErrorCode.PasswordChangeFailed, "Greška pri menjanju lozinke.");
            }

            var role = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Role);

            return(await CreateTokens(user, role, true));
        }
Esempio n. 8
0
        public async Task <bool> SaveImage(HttpContext context)
        {
            IFormFile file;

            try
            {
                file = context.Request.Form.Files.FirstOrDefault(f => f.Name == "file");
                if (file == null)
                {
                    throw new ErrorException(ErrorCode.ImageNotFound, "Slika nije pronađena.");
                }
            }
            catch (Exception)
            {
                throw new ErrorException(ErrorCode.ImageNotFound, "Slika nije pronađena.");
            }
            if (file.Length > 10000000)
            {
                throw new ErrorException(ErrorCode.ImageTooLarge, "Slika zauzima previše prostora.");
            }

            string ext   = Path.GetExtension(file.FileName);
            var    path1 = Path.Combine("Resources", "Images");
            var    path  = Path.Combine(path1, "Avatars");

            string userName  = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);
            string imageName = userName;

            var fullPath = Path.Combine(path, imageName);

            using (var stream = new FileStream(fullPath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            return(true);
        }
Esempio n. 9
0
        public async Task <Account> Get(HttpContext context)
        {
            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Korisnik ne postoji u sistemu.");
            }

            var role = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Role);

            return(new Account
            {
                Username = user.UserName,
                Email = user.Email,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Address = user.Address,
                PhoneNumber = user.PhoneNumber,
                Role = role
            });
        }
Esempio n. 10
0
        public async Task <OutProdavacNarudzbinaDTO> Get(Guid id, HttpContext context)
        {
            var narudzbina = await _db.Narudzbine.Include(n => n.Kupac).Include(n => n.Prodavac)
                             .Include(n => n.ListaElemenata).ThenInclude(k => k.Proizvod).FirstOrDefaultAsync(a => a.Id == id);

            if (narudzbina == null)
            {
                return(null);
            }

            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Prodavac ne postoji u sistemu.");
            }

            if (narudzbina.Prodavac.Id != user.Id && narudzbina.Kupac.Id != user.Id)
            {
                throw new ErrorException(ErrorCode.OrderAccessError, "Nemate pravo da pristupite ovoj narudzbini.");
            }

            var outProdavacNarudzbina = new OutProdavacNarudzbinaDTO
            {
                Id = narudzbina.Id,
                StatusNarudzbine     = narudzbina.StatusNarudzbine,
                VremeIsporukeUDanima = narudzbina.VremeIsporukeUDanima,
                Prodavac             = new Account
                {
                    Address     = narudzbina.Prodavac.Address,
                    Email       = narudzbina.Prodavac.Email,
                    FirstName   = narudzbina.Prodavac.FirstName,
                    LastName    = narudzbina.Prodavac.LastName,
                    PhoneNumber = narudzbina.Prodavac.PhoneNumber
                },
                Kupac = new Account
                {
                    Address     = narudzbina.Kupac.Address,
                    Email       = narudzbina.Kupac.Email,
                    FirstName   = narudzbina.Kupac.FirstName,
                    LastName    = narudzbina.Kupac.LastName,
                    PhoneNumber = narudzbina.Kupac.PhoneNumber
                },
                ListaElemenata           = new List <OutElementKorpeDTO>(),
                DatumNarudzbine          = narudzbina.DatumNarudzbine,
                DatumOdobrenjaNarudzbine = narudzbina.DatumPotvrdeNarudzbine
            };

            foreach (var el in narudzbina.ListaElemenata)
            {
                outProdavacNarudzbina.ListaElemenata.Add(new OutElementKorpeDTO
                {
                    Kolicina = el.Kolicina,
                    Proizvod = new OutProizvodDTO
                    {
                        Id              = el.Proizvod.Id,
                        Naziv           = el.Proizvod.Naziv,
                        Cena            = el.Proizvod.Cena,
                        Opis            = el.Proizvod.Opis,
                        NacinKoriscenja = el.Proizvod.NacinKoriscenja,
                        Prodavac        = null
                    }
                });
            }

            return(outProdavacNarudzbina);
        }
Esempio n. 11
0
        public async Task <List <OutOrderDTO> > Add(InOrderDTO model, HttpContext context)
        {
            if (model == null || model.ListaElemenata == null || model.ListaElemenata.Count == 0)
            {
                return(null);
            }

            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Prodavac ne postoji u sistemu.");
            }

            var listaNarudzbina = new List <Narudzbina>();

            foreach (var el in model.ListaElemenata)
            {
                var proizvod = _db.Proizvodi.Where(p => p.Id == el.Id)?.Include(i => i.Prodavac).FirstOrDefault();
                if (proizvod == null)
                {
                    continue;
                }
                var narudzbinaZaOvogProdavca = listaNarudzbina.FirstOrDefault(n => n.Prodavac.Id == proizvod.Prodavac.Id);
                if (narudzbinaZaOvogProdavca == null)
                {
                    narudzbinaZaOvogProdavca = new Narudzbina
                    {
                        Id             = Guid.NewGuid(),
                        ListaElemenata = new List <ElementKorpe>
                        {
                            new ElementKorpe
                            {
                                Id       = Guid.NewGuid(),
                                Kolicina = el.Kolicina,
                                Proizvod = proizvod
                            }
                        },
                        Kupac                = user,
                        StatusNarudzbine     = StatusNarudzbine.Nova,
                        VremeIsporukeUDanima = null,
                        Prodavac             = proizvod.Prodavac
                    };
                    listaNarudzbina.Add(narudzbinaZaOvogProdavca);
                }
                else
                {
                    narudzbinaZaOvogProdavca.ListaElemenata.Add(new ElementKorpe
                    {
                        Id       = Guid.NewGuid(),
                        Kolicina = el.Kolicina,
                        Proizvod = proizvod
                    });
                }
            }

            foreach (var narudzbina in listaNarudzbina)
            {
                narudzbina.DatumNarudzbine = DateTime.UtcNow;
                _db.Narudzbine.Add(narudzbina);
            }

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw new ErrorException(ErrorCode.DbError, "Greška pri čuvanju narudzbine u bazu podataka.");
            }

            var outListaNarudzbina = new List <OutOrderDTO>();

            foreach (var narudzbina in listaNarudzbina)
            {
                var outNar = new OutOrderDTO
                {
                    Id       = narudzbina.Id,
                    Prodavac = new Account
                    {
                        FirstName   = narudzbina.Prodavac.FirstName,
                        LastName    = narudzbina.Prodavac.LastName,
                        Email       = narudzbina.Prodavac.Email,
                        PhoneNumber = narudzbina.Prodavac.PhoneNumber
                    },
                    ListaElemenata = new List <OutElementKorpeDTO>()
                };
                foreach (var el in narudzbina.ListaElemenata)
                {
                    outNar.ListaElemenata.Add(new OutElementKorpeDTO
                    {
                        Kolicina = el.Kolicina,
                        Proizvod = new OutProizvodDTO
                        {
                            Id              = el.Proizvod.Id,
                            Naziv           = el.Proizvod.Naziv,
                            Cena            = el.Proizvod.Cena,
                            Opis            = el.Proizvod.Opis,
                            NacinKoriscenja = el.Proizvod.NacinKoriscenja,
                            Prodavac        = null
                        }
                    });
                }
                outListaNarudzbina.Add(outNar);
            }

            return(outListaNarudzbina);
        }
Esempio n. 12
0
        public async Task <List <OutProdavacNarudzbinaDTO> > GetAllForBuyer(HttpContext context)
        {
            string userName = TokensHelper.GetClaimFromJwt(context, ClaimTypes.Name);

            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                throw new ErrorException(ErrorCode.UserNotFound, "Prodavac ne postoji u sistemu.");
            }

            var narudzbine = await _db.Narudzbine.Include(n => n.Kupac).Include(n => n.Prodavac)
                             .Include(n => n.ListaElemenata).ThenInclude(k => k.Proizvod).Where(k => k.Kupac == user)?.ToListAsync();

            if (narudzbine == null)
            {
                return(null);
            }

            List <OutProdavacNarudzbinaDTO> outProdavacNarudzbine = new List <OutProdavacNarudzbinaDTO>();

            foreach (var narudzbina in narudzbine)
            {
                var outProdavacNarudzbina = new OutProdavacNarudzbinaDTO
                {
                    Id = narudzbina.Id,
                    StatusNarudzbine     = narudzbina.StatusNarudzbine,
                    VremeIsporukeUDanima = narudzbina.VremeIsporukeUDanima,
                    Kupac = new Account
                    {
                        Address     = narudzbina.Kupac.Address,
                        Email       = narudzbina.Kupac.Email,
                        FirstName   = narudzbina.Kupac.FirstName,
                        LastName    = narudzbina.Kupac.LastName,
                        PhoneNumber = narudzbina.Kupac.PhoneNumber
                    },
                    Prodavac = new Account
                    {
                        Address     = narudzbina.Prodavac.Address,
                        Email       = narudzbina.Prodavac.Email,
                        FirstName   = narudzbina.Prodavac.FirstName,
                        LastName    = narudzbina.Prodavac.LastName,
                        PhoneNumber = narudzbina.Prodavac.PhoneNumber
                    },
                    ListaElemenata           = new List <OutElementKorpeDTO>(),
                    DatumNarudzbine          = narudzbina.DatumNarudzbine,
                    DatumOdobrenjaNarudzbine = narudzbina.DatumPotvrdeNarudzbine
                };
                foreach (var el in narudzbina.ListaElemenata)
                {
                    outProdavacNarudzbina.ListaElemenata.Add(new OutElementKorpeDTO
                    {
                        Kolicina = el.Kolicina,
                        Proizvod = new OutProizvodDTO
                        {
                            Id              = el.Proizvod.Id,
                            Naziv           = el.Proizvod.Naziv,
                            Cena            = el.Proizvod.Cena,
                            Opis            = el.Proizvod.Opis,
                            NacinKoriscenja = el.Proizvod.NacinKoriscenja,
                            Prodavac        = null
                        }
                    });
                }
                outProdavacNarudzbine.Add(outProdavacNarudzbina);
            }

            return(outProdavacNarudzbine);
        }