Пример #1
0
        public MusicResult UpdateSound(Sound sound)
        {
            using (_context)
            {
                try
                {
                    string id     = Convert.ToString(sound.Id);
                    Sound  record = SoundRepository.FindBy(x => x.Id == id).FirstOrDefault();

                    if (record == null)
                    {
                        MusicResult errorResult = new MusicResult
                        {
                            Id           = record.Id,
                            IsSuccessful = false,
                            Message      = $"An unexpected error on finding the sound. Can't find the sound with id : {id}"
                        };

                        return(errorResult);
                    }

                    record.Id       = sound.Id;
                    record.FileName = sound.FileName;
                    record.FilePath = sound.FilePath;
                    record.FileSize = sound.FileSize;
                    record.Modified = DateTime.Now;

                    _context.Update(record);

                    _context.SaveChanges();

                    MusicResult result = new MusicResult
                    {
                        Id           = id,
                        IsSuccessful = true,
                        Message      = "Update sound successfully"
                    };

                    return(result);
                }
                catch (Exception exception)
                {
                    MusicResult result = new MusicResult
                    {
                        Id           = sound.Id,
                        IsSuccessful = false,
                        Message      = $"An unexpected error on updating sound: {exception.Message}"
                    };

                    return(result);
                }
            }
        }
Пример #2
0
        public MusicResult DeleteSound(string id)
        {
            using (_context)
            {
                try
                {
                    var record = SoundRepository.FindBy(x => x.Id == id).FirstOrDefault();
                    if (record == null)
                    {
                        MusicResult errorResult = new MusicResult
                        {
                            Id           = record.Id,
                            IsSuccessful = false,
                            Message      = "There isn't a record in the database. "
                        };
                        return(errorResult);
                    }
                    _context.Remove(record);

                    _context.SaveChanges();

                    MusicResult result = new MusicResult
                    {
                        Id           = id,
                        IsSuccessful = true,
                        Message      = "Delete sound successful"
                    };

                    return(result);
                }
                catch (Exception exception)
                {
                    MusicResult result = new MusicResult
                    {
                        Id           = id,
                        IsSuccessful = false,
                        Message      = $"An unexpected error on deleting sound: {exception.Message}"
                    };

                    return(result);
                }
            }
        }