Пример #1
0
        public async Task <IActionResult> UpdateItem([FromBody] LeaderboardItem leaderboardItem)
        {
            try
            {
                if (!string.IsNullOrEmpty(leaderboardItem.RouteData) && leaderboardItem.RouteData.Length > 0)
                {
                    CheckValidXml(leaderboardItem.RouteData, leaderboardItem.RouteDataType);
                }

                LeaderboardItem entry = await _context.Leaderboard.AsNoTracking().FirstOrDefaultAsync(x => x.Id == leaderboardItem.Id);

                if (entry == null)
                {
                    leaderboardItem.Status = LeaderboardEntryState.Pending;
                    await _context.Leaderboard.AddAsync(leaderboardItem);
                }
                else
                {
                    //these two items stay empty on client side, so transfer over to updated entry.
                    leaderboardItem.Id        = entry.Id;
                    leaderboardItem.RouteData = entry.RouteData;

                    _context.Leaderboard.Update(leaderboardItem);
                }

                await _context.SaveChangesAsync();

                return(Ok(new BasicHttpResponse <object> {
                    Ok = true, Data = leaderboardItem
                }));
            }
            catch (Exception e)
            {
                return(Ok(new BasicHttpResponse <object> {
                    Ok = false, Message = Shared.Helpers.Functions.ExceptionMessage(e)
                }));
            }
        }
Пример #2
0
        public async Task <IActionResult> UpdateUser([FromBody] UserAccount user)
        {
            try
            {
                var u = _context.Users.FirstOrDefault(x => x.Email == user.Email);

                DateTime now = DateTime.Now;

                if (u == null)
                {
                    // New user. Assign user name and save.
                    int idNum = _context.Users.Any()
                        ? _context.Users.Max(x => x.Id) + 1000
                        : 1000;

                    u = new UserAccount
                    {
                        Username     = $"FivePeaker_{idNum}",
                        Email        = user.Email,
                        CreationDate = now,
                        LastLogin    = now
                    };

                    await _context.Users.AddAsync(u);
                }
                else
                {
                    u.LastLogin = now;

                    if (!string.IsNullOrEmpty(user.Username) && user.Username != u.Username)
                    {
                        //User is setting custom username
                        if (_context.Users.Any(x => x.Username == user.Username))
                        {
                            throw new Exception("Username is taken!");
                        }

                        if (user.Username.Length < 3)
                        {
                            throw new Exception("Username too short!");
                        }

                        if (user.Username.Length >= 30)
                        {
                            throw new Exception("Username too long!");
                        }

                        u.Username = user.Username;
                    }

                    _context.Entry(u).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }

                await _context.SaveChangesAsync();

                return(Ok(new BasicHttpResponse <UserAccount> {
                    Ok = true, Data = u
                }));
            }
            catch (Exception e)
            {
                return(Ok(new BasicHttpResponse <UserAccount> {
                    Ok = false, Message = e.Message
                }));
            }
        }
Пример #3
0
        public async Task <IActionResult> AddEditBlog([FromBody] BlogPost blog)
        {
            try
            {
                if (blog == null)
                {
                    throw new Exception("No blog posted!");
                }

                BlogPost editing = _context.Blogs.FirstOrDefault(b => b.Id == blog.Id);

                if (editing != null)
                {
                    editing.Active   = blog.Active;
                    editing.Author   = blog.Author;
                    editing.Content  = blog.Content;
                    editing.PostDate = blog.PostDate;
                    editing.Tags     = blog.Tags;
                    editing.Title    = blog.Title;
                    editing.Views    = blog.Views;
                    _context.Entry(editing).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }
                else
                {
                    editing = new BlogPost
                    {
                        Active   = blog.Active,
                        Author   = blog.Author,
                        Content  = blog.Content,
                        PostDate = blog.PostDate,
                        Tags     = blog.Tags,
                        Title    = blog.Title,
                        Views    = blog.Views
                    };

                    await _context.Blogs.AddAsync(editing);
                }

                //Header image added/replaced.
                if (blog.HeaderImageBase64.Length > 0)
                {
                    Image img = Image.Load(Convert.FromBase64String(blog.HeaderImageBase64));

                    JpegEncoder encoder = new JpegEncoder();

                    await using MemoryStream mem = new MemoryStream();

                    encoder.Quality = 100;

                    await img.SaveAsJpegAsync(mem, encoder);

                    int q = 100;
                    while (mem.Length >= 400000 && q > 1)
                    {
                        q -= 10;
                        encoder.Quality = q;
                        mem.Position    = 0;
                        img             = await Image.LoadAsync(mem);

                        //start again
                        mem.SetLength(0);
                        await img.SaveAsJpegAsync(mem, encoder);
                    }

                    editing.HeaderImageBase64 = Convert.ToBase64String(mem.ToArray());


                    if (mem.Length > 500000)
                    {
                        throw new Exception($"Could not compress image enough! Try uploading a smaller image");
                    }
                }

                await _context.SaveChangesAsync();

                return(Ok(new BasicHttpResponse <object> {
                    Ok = true
                }));
            }
            catch (Exception e)
            {
                return(Ok(new BasicHttpResponse <object> {
                    Ok = false, Message = e.Message
                }));
            }
        }