From a3d82ad01fb2100c4504661c44e6f447e85370fb Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 22 Apr 2021 16:07:26 +0430 Subject: [PATCH] #106 site banner api is now complete --- RMuseum/Controllers/GanjoorController.cs | 74 +++++++++++++++-- RMuseum/RMuseum.xml | 38 ++++++++- RMuseum/Services/IGanjoorService.cs | 14 +++- .../Services/Implementation/GanjoorService.cs | 79 ++++++++++++++++++- 4 files changed, 193 insertions(+), 12 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 57736faa..92408c7b 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -932,13 +932,13 @@ namespace RMuseum.Controllers } /// - /// add site banner (send form) with these feilds: filename, alt, url and an image attachment + /// add site banner (send form) with these fields: filename, alt, url and an image attachment /// /// [HttpPost] [Route("site/banner")] - [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Banners)] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorSiteBanner))] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorSiteBannerViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.Unauthorized)] public async Task AddSiteBanner() @@ -965,15 +965,13 @@ namespace RMuseum.Controllers return BadRequest("a single image is not provided"); } using Stream stream = Request.Form.Files[0].OpenReadStream(); - RServiceResult res = await _ganjoorService.AddSiteBanner(stream, filename.ToString(), alt.ToString(), alt.ToString(), false); + RServiceResult res = await _ganjoorService.AddSiteBanner(stream, filename.ToString(), alt.ToString(), alt.ToString(), false); if (!string.IsNullOrEmpty(res.ExceptionString)) { return BadRequest(res.ExceptionString); } - - return Ok(res.Result); } catch (Exception exp) @@ -1057,6 +1055,70 @@ namespace RMuseum.Controllers } } + /// + /// get all defined site banners + /// + /// + + [HttpGet] + [Route("site/banners")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Banners)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorSiteBannerViewModel[]))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Unauthorized)] + public async Task GetSiteBanners() + { + try + { + RServiceResult res = await _ganjoorService.GetSiteBanners(); + + if (!string.IsNullOrEmpty(res.ExceptionString)) + { + return BadRequest(res.ExceptionString); + } + return Ok(res.Result); + } + catch (Exception exp) + { + return BadRequest(exp.ToString()); + } + } + + /// + /// get a random active site banner + /// + /// + [HttpGet] + [Route("site/banner")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Banners)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorSiteBannerViewModel))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Unauthorized)] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + public async Task GetARandomActiveSiteBanner() + { + try + { + RServiceResult res = await _ganjoorService.GetARandomActiveSiteBanner(); + + if (!string.IsNullOrEmpty(res.ExceptionString)) + { + return BadRequest(res.ExceptionString); + } + + if(res.Result == null) + { + return NotFound(); + } + + return Ok(res.Result); + } + catch (Exception exp) + { + return BadRequest(exp.ToString()); + } + } + /// /// Ganjoor Service /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 503ccc51..1d3aeef5 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -885,7 +885,7 @@ - add site banner (send form) with these feilds: filename, alt, url and an image attachment + add site banner (send form) with these fields: filename, alt, url and an image attachment @@ -904,6 +904,18 @@ + + + get all defined site banners + + + + + + get a random active site banner + + + Ganjoor Service @@ -6768,6 +6780,18 @@ + + + get site banners + + + + + + get a random site banner + + + get a random poem from hafez @@ -7816,6 +7840,18 @@ + + + get site banners + + + + + + get a random site banner + + + Database Context diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 42c04303..fe19c9c4 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -267,7 +267,7 @@ namespace RMuseum.Services /// /// /// - Task> AddSiteBanner(Stream imageStream, string fileName, string alternateText, string targetUrl, bool active); + Task> AddSiteBanner(Stream imageStream, string fileName, string alternateText, string targetUrl, bool active); /// /// modify site banner @@ -286,6 +286,18 @@ namespace RMuseum.Services /// Task> DeleteSiteBanner(int id); + /// + /// get site banners + /// + /// + Task> GetSiteBanners(); + + /// + /// get a random site banner + /// + /// + Task> GetARandomActiveSiteBanner(); + /// /// get a random poem from hafez /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 0139403b..a52d0ee7 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -1888,7 +1888,7 @@ namespace RMuseum.Services.Implementation /// /// /// - public async Task> AddSiteBanner(Stream imageStream, string fileName, string alternateText, string targetUrl, bool active) + public async Task> AddSiteBanner(Stream imageStream, string fileName, string alternateText, string targetUrl, bool active) { try { @@ -1896,7 +1896,7 @@ namespace RMuseum.Services.Implementation if (!string.IsNullOrEmpty(image.ExceptionString)) { - return new RServiceResult(null, image.ExceptionString); + return new RServiceResult(null, image.ExceptionString); } GanjoorSiteBanner banner = new GanjoorSiteBanner() @@ -1910,12 +1910,22 @@ namespace RMuseum.Services.Implementation _context.GanjoorSiteBanners.Add(banner); await _context.SaveChangesAsync(); - return new RServiceResult(banner); + return new RServiceResult + ( + new GanjoorSiteBannerViewModel() + { + Id = banner.Id, + ImageUrl = $"api/rimages/{banner.RImageId}.jpg", + AlternateText = banner.AlternateText, + TargetUrl = banner.TargetUrl, + Active = banner.Active + } + ); } catch(Exception exp) { - return new RServiceResult(null, exp.ToString()); + return new RServiceResult(null, exp.ToString()); } } @@ -1978,6 +1988,67 @@ namespace RMuseum.Services.Implementation } } + /// + /// get site banners + /// + /// + public async Task> GetSiteBanners() + { + try + { + return new RServiceResult( + await _context.GanjoorSiteBanners + .Select(b => new GanjoorSiteBannerViewModel() + { + Id = b.Id, + ImageUrl = $"api/rimages/{b.RImageId}.jpg", + AlternateText = b.AlternateText, + TargetUrl = b.TargetUrl, + Active = b.Active + }).ToArrayAsync() + ); + } + catch(Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + + /// + /// get a random site banner + /// + /// + public async Task> GetARandomActiveSiteBanner() + { + try + { + int[] idSet = await _context.GanjoorSiteBanners.Where(b => b.Active == true).Select(b => b.Id).ToArrayAsync(); + if (idSet.Length == 0) + return new RServiceResult(null);//not found + + Random rnd = new Random(DateTime.Now.Millisecond); + int id = idSet[rnd.Next(0, idSet.Length - 1)]; + + return new RServiceResult( + await _context.GanjoorSiteBanners + .Where(b => b.Id == id) + .Select(b => new GanjoorSiteBannerViewModel() + { + Id = b.Id, + ImageUrl = $"api/rimages/{b.RImageId}.jpg", + AlternateText = b.AlternateText, + TargetUrl = b.TargetUrl, + Active = b.Active + }).SingleAsync() + ); + + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// Database Context ///