/// <summary> /// If exist record - updates Count, if no - inserts new record /// </summary> /// <param name="backetData"></param> public void UpdateBacket(BacketData backetData) { using (var con = OpenConnection()) { string sqlQuery = $"UPDATE Backet SET Count = Count + {backetData.Count} WHERE UserId = {backetData.UserId} AND ToyId = {backetData.ToyId};" + $"INSERT INTO Backet (UserId, ToyId, Count) SELECT {backetData.UserId}, {backetData.ToyId}, {backetData.Count} WHERE NOT EXISTS(SELECT 1 FROM Backet WHERE UserId = {backetData.UserId} AND ToyId = {backetData.ToyId});"; con.Execute(sqlQuery); } }
/// <summary> /// Added new record to data base or updated existing /// </summary> /// <param name="backetData"></param> /// <returns></returns> public Errors UpdateBacket(BacketData backetData) { try { _dbManager.UpdateBacket(backetData); return(Errors.OK); } catch (Exception ex) { _logger.Error(ex.Message); return(Errors.DATA_BASE_ERROR); } }
public IHttpActionResult UpdateBacket([FromBody] BacketData backetData) { var validating = this.ValidatingBacketData(backetData.UserId, backetData.ToyId); if (validating != Errors.OK) { return(Content(HttpStatusCode.BadRequest, new ErrorResponse(validating))); } var result = _backetManager.UpdateBacket(backetData); if (result == Errors.OK) { return(Content(HttpStatusCode.OK, "Success")); } return(Content(HttpStatusCode.BadRequest, result)); }
public void UpdateBacket_Return_DATA_BASE_ERROR() { // Arrange var mockDb = new Mock <IDataBaseManager>(); var mockLogger = new Mock <ILogger>(); BacketData backetData = new BacketData() { UserId = 1, ToyId = 1, Count = 3 }; mockDb.Setup(db => db.UpdateBacket(It.IsAny <BacketData>())).Throws(new Exception()); mockLogger.Setup(db => db.Error(It.IsAny <string>())); var manager = new BacketManager(mockDb.Object, mockLogger.Object); // Act var result = manager.UpdateBacket(backetData); // Assert Assert.AreEqual(Errors.DATA_BASE_ERROR, result); }