Ejemplo n.º 1
0
        public async Task <OperationResult> AddMessage(int currentUserId, MessagePostDto message)
        {
            var messageModel = new Message()
            {
                Content     = message.Content,
                DateCreated = DateTime.Now,
                IsDelivered = false,
                ReceiverId  = message.ReceiverId,
                SenderId    = currentUserId
            };
            await repository.AddAsync(messageModel);

            var response = await repository.SaveChangesAsync();

            if (response > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true, Payload = messageModel
                });
            }
            else
            {
                return(OtherServices.GetIncorrectDatabaseConnectionResult());
            }
        }
Ejemplo n.º 2
0
        public async Task <OperationResult> CreateSystem(SystemCreateOrUpdateDto system)
        {
            var systemModel = mapper.Map <SystemCreateOrUpdateDto, Models.System>(system);
            var doesExists  = await repository.GetSameSystem(systemModel);

            if (doesExists != null)
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Payload = doesExists
                });
            }

            await repository.AddAsync(systemModel);

            var repoResult = await repository.SaveChangesAsync();

            if (repoResult > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true,
                    Payload = mapper.Map <Models.System, SystemDto>(systemModel)
                });
            }
            else
            {
                return(OtherServices.GetIncorrectDatabaseConnectionResult());
            }
        }
Ejemplo n.º 3
0
        public async Task <OperationResult> ChangeUserEmail(int userId, string email)
        {
            var user = await userRepository.GetAsync(userId);

            if (user.Email.Equals(email))
            {
                return(new OperationResult
                {
                    IsSuccessful = true
                });
            }

            user.Email = email;
            var repoResult = await userRepository.SaveChangesAsync();

            if (repoResult > 0)
            {
                return(new OperationResult
                {
                    IsSuccessful = true
                });
            }

            return(OtherServices.GetIncorrectDatabaseConnectionResult());
        }
Ejemplo n.º 4
0
        public async Task <OperationResult> CreateGenre(GenreCreateOrUpdateDto genre)
        {
            var genreModel = mapper.Map <GenreCreateOrUpdateDto, Genre>(genre);
            var doesExists = await repository.FindAsync(g => g.Value == genreModel.Value);

            if (doesExists.Any())
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Payload = doesExists
                });
            }

            await repository.AddAsync(genreModel);

            var repoResult = await repository.SaveChangesAsync();

            if (repoResult > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true,
                    Payload = genreModel
                });
            }
            else
            {
                return(OtherServices.GetIncorrectDatabaseConnectionResult());
            }
        }
Ejemplo n.º 5
0
        public async Task <OperationResult> ChangeUserPhone(int userId, string phone)
        {
            var user = await userRepository.GetAsync(userId);

            if (user.PhoneNumber != null && user.PhoneNumber.Equals(phone))
            {
                return(new OperationResult
                {
                    IsSuccessful = true
                });
            }

            user.PhoneNumber = phone;
            var repoResult = await userRepository.SaveChangesAsync();

            if (repoResult > 0)
            {
                return(new OperationResult
                {
                    IsSuccessful = true
                });
            }

            return(OtherServices.GetIncorrectDatabaseConnectionResult());
        }
Ejemplo n.º 6
0
        public async Task <OperationResult> ChangeUserDescription(int userId, string description)
        {
            var user = await userRepository.GetAsync(userId);

            if (user.Description != null && user.Description.Equals(description))
            {
                return(new OperationResult
                {
                    IsSuccessful = true
                });
            }

            user.Description = description;
            var repoResult = await userRepository.SaveChangesAsync();

            if (repoResult > 0)
            {
                return(new OperationResult
                {
                    IsSuccessful = true
                });
            }

            return(OtherServices.GetIncorrectDatabaseConnectionResult());
        }
Ejemplo n.º 7
0
        public async Task <OperationResult> CreateAnnouncement(AnnouncementSaveDto announcement, string login)
        {
            var currentUser = await userRepository.GetUserByLogin(login);

            var announcementModel = mapper.Map <AnnouncementSaveDto, Announcement>(announcement);

            announcementModel.UserId      = currentUser.Id;
            announcementModel.DateCreated = DateTime.Now;
            await repository.AddAsync(announcementModel);

            var result = await repository.SaveChangesAsync();

            if (result > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true,
                    Payload = announcementModel
                });
            }
            else
            {
                return(OtherServices.GetIncorrectDatabaseConnectionResult());
            }
        }
Ejemplo n.º 8
0
        public async Task <OperationResult> DeleteAnnouncement(int id)
        {
            var entity = await repository.GetAsync(id);

            if (entity == null)
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Announcement was not found"
                });
            }
            repository.Remove(entity);
            var result = await repository.SaveChangesAsync();

            if (result > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true
                });
            }
            else
            {
                return(OtherServices.GetIncorrectDatabaseConnectionResult());
            }
        }
Ejemplo n.º 9
0
        public async Task <OperationResult> AddObservedUser(ObservedUsersRelationshipDto pair)
        {
            var observingUser = await GetUserById(pair.ObservingUserId);

            if (observingUser == null)
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Observing user not found!"
                });
            }
            var observedUser = await GetUserById(pair.ObservedUserId);

            if (observedUser == null)
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Observed user not found!"
                });
            }

            var currentList = await GetObservedUsersForUser(pair.ObservingUserId);

            var ids = currentList.Select(u => u.Id);

            if (ids.Contains(pair.ObservedUserId))
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Payload = pair
                });
            }

            await userRepository.AddObsersvedUser(pair.ObservingUserId, pair.ObservedUserId);

            var result = await userRepository.SaveChangesAsync();

            if (result > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true,
                    Payload = pair
                });
            }
            else
            {
                return(OtherServices.GetIncorrectDatabaseConnectionResult());
            }
        }
Ejemplo n.º 10
0
        public async Task <OperationResult> EditSystem(int id, SystemCreateOrUpdateDto system)
        {
            var systemModel = mapper.Map <SystemCreateOrUpdateDto, Models.System>(system);
            var doesExists  = await repository.GetSameSystem(systemModel);

            if (doesExists != null && doesExists.Id == id)
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Payload = doesExists
                });
            }

            var systemInDb = await repository.GetAsync(id);

            if (systemInDb != null)
            {
                systemInDb.Model        = system.Model;
                systemInDb.Manufacturer = system.Manufacturer;
                var repoResult = await repository.SaveChangesAsync();

                if (repoResult > 0)
                {
                    return(new OperationResult()
                    {
                        IsSuccessful = true,
                        Payload = systemInDb
                    });
                }
                else
                {
                    return(OtherServices.GetIncorrectDatabaseConnectionResult());
                }
            }
            else
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Obiekt nie istnieje"
                });
            }
        }
Ejemplo n.º 11
0
        public async Task <OperationResult> EditGenre(int id, GenreCreateOrUpdateDto genre)
        {
            var doesExists = await repository.FindAsync(g => g.Value == genre.Value && g.Id != id);

            if (doesExists.Any())
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Payload = doesExists
                });
            }

            var genreInDb = await repository.GetAsync(id);

            if (genreInDb != null)
            {
                genreInDb.Value = genre.Value;
                var repoResult = await repository.SaveChangesAsync();

                if (repoResult > 0)
                {
                    return(new OperationResult()
                    {
                        IsSuccessful = true,
                        Payload = mapper.Map <Genre, GenreDto>(genreInDb)
                    });
                }
                else
                {
                    return(OtherServices.GetIncorrectDatabaseConnectionResult());
                }
            }
            else
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Obiekt nie istnieje"
                });
            }
        }
Ejemplo n.º 12
0
        public async Task <OperationResult> EditAnnouncement(int id, AnnouncementSaveDto announcement)
        {
            var announcementInDb = await repository.GetAsync(id);

            if (announcementInDb == null)
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true
                });
            }

            if (announcementInDb.Title.Equals(announcement.Title) &&
                announcementInDb.Content.Equals(announcement.Content))
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Nothing has changed operation aborted",
                    Payload = announcement
                });
            }

            announcementInDb.Content = announcement.Content;
            announcementInDb.Title   = announcement.Title;

            var result = await repository.SaveChangesAsync();

            if (result > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true
                });
            }
            else
            {
                return(OtherServices.GetIncorrectDatabaseConnectionResult());
            }
        }
Ejemplo n.º 13
0
        public async Task <OperationResult> DeleteSystem(int id)
        {
            var systemInDb = await repository.GetSystemWithItems(id);

            if (systemInDb != null)
            {
                if (systemInDb.AdvertisementItems.Any())
                {
                    return(new OperationResult()
                    {
                        IsSuccessful = false,
                        IsClientError = true,
                        Message = "Istnieją ogłoszenia związane z tym systemem, usuń je przed usunięciem systemu",
                        Payload = systemInDb.AdvertisementItems
                    });
                }
                repository.Remove(systemInDb);
                var repoResult = await repository.SaveChangesAsync();

                if (repoResult > 0)
                {
                    return(new OperationResult()
                    {
                        IsSuccessful = true
                    });
                }
                else
                {
                    return(OtherServices.GetIncorrectDatabaseConnectionResult());
                }
            }
            else
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Object does not exist in database"
                });
            }
        }
Ejemplo n.º 14
0
        public async Task <OperationResult> DeleteGenre(int id)
        {
            var genreInDb = await repository.GetGenreWithGames(id);

            if (genreInDb != null)
            {
                if (genreInDb.Games.Any())
                {
                    return(new OperationResult()
                    {
                        IsSuccessful = false,
                        IsClientError = true,
                        Message = "Istnieją ogłoszenia związane z tym gatunkiem, usuń je przed usunięciem gatunku",
                        Payload = genreInDb.Games
                    });
                }
                repository.Remove(genreInDb);
                var repoResult = await repository.SaveChangesAsync();

                if (repoResult > 0)
                {
                    return(new OperationResult()
                    {
                        IsSuccessful = true
                    });
                }
                else
                {
                    return(OtherServices.GetIncorrectDatabaseConnectionResult());
                }
            }
            else
            {
                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Object does not exist in database"
                });
            }
        }
Ejemplo n.º 15
0
        public async Task <OperationResult> ChangeUserPhoto(int userId, IFormFile photo)
        {
            var user = await userRepository.GetAsync(userId);

            if (photo == null && user.PhotoId.HasValue)
            {
                var oldPhoto = await photoRepository.GetAsync(user.PhotoId.Value);

                if (oldPhoto != null)
                {
                    File.Delete(oldPhoto.Path);
                }
                photoRepository.Remove(oldPhoto);
                user.PhotoId = null;
                var repoRes = await userRepository.SaveChangesAsync();

                if (repoRes > 0)
                {
                    return(new OperationResult {
                        IsSuccessful = true
                    });
                }
                throw new DataException();
            }

            if (photo == null)
            {
                return(new OperationResult {
                    IsSuccessful = true
                });
            }

            var fileId    = Guid.NewGuid().ToString("N").ToUpper();
            var directory = @"photos/user" + userId;

            Directory.CreateDirectory(directory);

            var path = Path.Combine(directory, fileId);

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                await photo.CopyToAsync(fileStream);
            }
            var newPhoto = new Photo {
                DateCreated = DateTime.Now, Path = path
            };

            if (user.PhotoId.HasValue)
            {
                var oldPhoto = await photoRepository.GetAsync(user.PhotoId.Value);

                if (oldPhoto != null)
                {
                    File.Delete(oldPhoto.Path);
                }
                photoRepository.Remove(oldPhoto);
                user.PhotoId = null;
                user.Photo   = null;
                var repoRes = await userRepository.SaveChangesAsync();

                if (repoRes == 0)
                {
                    File.Delete(path);
                    throw new DataException();
                }
            }

            await photoRepository.AddAsync(newPhoto);

            user.Photo = newPhoto;
            var repoResult = await userRepository.SaveChangesAsync();

            if (repoResult > 0)
            {
                return(new OperationResult
                {
                    IsSuccessful = true
                });
            }
            File.Delete(path);
            return(OtherServices.GetIncorrectDatabaseConnectionResult());
        }
Ejemplo n.º 16
0
        public async Task <OperationResult> DeleteAdvertisement(int userId, int adId, string message = null)
        {
            var ad = await repository.GetAsync(adId);

            if (ad.UserId != userId)
            {
                var user = await userRepository.GetAsync(userId);

                if (user.Role.Equals("Admin"))
                {
                    if (message == null)
                    {
                        return(new OperationResult()
                        {
                            IsSuccessful = false,
                            IsClientError = true,
                            Message = "Proszę dodać wiadomość !"
                        });
                    }

                    var otherUser = await userRepository.GetAsync(ad.UserId);

                    var text = string.Format(
                        @"Witaj. </br> Twoje ogłoszenie z serwisu Games4Trade o tytule: '{0}' zostało usunięte. Oto powód usunięcia ogłoszenia:<br>{1}",
                        ad.Title, message);
                    var emailResult = await OtherServices.SendEmail(otherUser.Email, "Wiadomość o usunięciu ogłoszenia.", text);

                    if (emailResult)
                    {
                        var repoResult = await RemoveAdWithPhotos(ad);

                        if (repoResult > 0)
                        {
                            return(new OperationResult()
                            {
                                IsSuccessful = true
                            });
                        }

                        return(OtherServices.GetIncorrectDatabaseConnectionResult());
                    }
                    return(new OperationResult()
                    {
                        IsSuccessful = false,
                        IsClientError = false
                    });
                }

                return(new OperationResult()
                {
                    IsSuccessful = false,
                    IsClientError = true,
                    Message = "Tylko administrator może usunąć cudze ogłoszenie!"
                });
            }

            var result = await RemoveAdWithPhotos(ad);

            if (result > 0)
            {
                return(new OperationResult()
                {
                    IsSuccessful = true
                });
            }

            return(new OperationResult()
            {
                IsSuccessful = false, IsClientError = true
            });
        }